Parallax is a visual effect where items closer to the viewer move faster than items in the background. Parallax creates a feeling of depth, perspective, and movement. In a UWP app, we can use the ParallaxView control to create a parallax effect. For ParallaxView control, we can set the VerticalShift (vertical scrolling) and HorizontalShift (horizontal scrolling).
Reading this article, you can learn how to use ParallaxView Control in Universal Windows Apps development with XAML and Visual C#.
The following important tools are required for developing UWP,
Step 1
Open Visual Studio 2017. Go to Start -> New Project-> Windows Universal (under Visual C#) -> Blank App (Universal Windows). Give a suitable name for your app (UWPParallaxView) and click OK.
After choosing the target and minimum platform version that your Windows Universal application will support, the Project creates App.xaml and MainPage.xaml.
Step 2
Add the following controls in design window for ParallaxView control view. Add an image in Assets folder for ParallaxView background image.
Add the TextBlock Controls and change the Name and Text property. Add the ParallaxView control with Image Control and set the Source and VerticalShift properties for vertical scrolling,
- <ParallaxView Source="{x:Bind LVParallax}" VerticalShift="50">
- <Image x:Name="BGImage1" Source="Assets/Blue Flowers.jpg" Stretch="UniformToFill"/>
- </ParallaxView>
Add ListView Control and set the Content.
- <ListView x:Name="LVParallax" VerticalContentAlignment="Top">
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- </ListView>
For horizontal scrolling, add one more ParallaxView control with Image Control, set the Source and HorizontalShift properties and add ListView Control and set the Content.
- <ParallaxView Source="{x:Bind LVParallaxHor}" HorizontalShift="50">
- <Image x:Name="BGImage2" Source="Assets/Blue Flowers.jpg" Stretch="UniformToFill" />
- </ParallaxView>
Add ListView Control and set the Horizontal Scroll Viewer properties and Content.
- <ListView x:Name="LVParallaxHor" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollMode="Enabled" ScrollViewer.VerticalScrollMode="Disabled" >
Automatically, the following code will be generated in XAML code view while we are done in the design view.
- <Page x:Class="UWPParallaxView.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:UWPParallaxView" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">
- <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Margin="138,129,0,0">
- <Grid Margin="57,129,989,143">
- <ParallaxView Source="{x:Bind LVParallax}" VerticalShift="50">
- <Image x:Name="BGImage1" Source="Assets/Blue Flowers.jpg" Stretch="UniformToFill" /> </ParallaxView>
- <ListView x:Name="LVParallax" VerticalContentAlignment="Top">
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- </ListView>
- </Grid>
- <TextBlock x:Name="tblHorizontal" HorizontalAlignment="Left" Margin="505,97,0,0" Text="Horizontal Shift" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18" FontWeight="Bold" />
- <Grid Margin="470,135,576,137">
- <ParallaxView Source="{x:Bind LVParallaxHor}" HorizontalShift="50">
- <Image x:Name="BGImage2" Source="Assets/Blue Flowers.jpg" Stretch="UniformToFill" /> </ParallaxView>
- <ListView x:Name="LVParallaxHor" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollMode="Enabled" ScrollViewer.VerticalScrollMode="Disabled">
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- <x:String>Parallax is a visual effect where items closer to the viewer move faster than items in the background.</x:String>
- </ListView>
- </Grid>
- <TextBlock x:Name="tblTitle" HorizontalAlignment="Left" Margin="324,47,0,0" Text="Parallax View in UWP" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="20" FontWeight="Bold" />
- <TextBlock x:Name="tblVertical" HorizontalAlignment="Left" Margin="99,94,0,0" Text="Vertical Shift" TextWrapping="Wrap" VerticalAlignment="Top" FontWeight="Bold" FontSize="18" /> </Grid>
- </Page>
Step 3
Deploy your app on local machine. The output of the UWPParallaxView is shown below.
For vertical scrolling,
For horizontal scrolling,
Summary
Now, you have successfully tested ParallaxView control in XAML and UWP environment using Visual Studio 2017.