Setting Canvas Background As FlipView Selected Image In UWP

Before reading this article, please go through the article's links, given below-

  1. Introduction To Universal Windows Platform (UWP) App Development Using Windows 10 And Visual Studio 2015
  2. How To Use FlipView Control In Universal Application Development With XAML And C#

Reading this article, you can learn, how to set the canvas background as Flipview selected image, using FlipView Control and Canvas Control in Universal Windows apps development with XAML and Visual C#.

The following important tools required to develop UWP are-

  1. Windows 10 (Recommended).
  2. Visual Studio 2015 Community Edition (It is a free software, available online).

Now, we can discuss step by step app development.

Step1- Open Visual Studio 2015 -> Start -> New Project-> Select Universal (under Visual C#->Windows)-> Blank app -> Give the suitable name for your app (FVimgtoCanBG) ->OK.

New Project

Step 2- Choose the Target and minimum platform version your Windows Universal Application will support. Afterwards, the project creates App.xaml and MainPage.xaml.

version

Step 3- Open (double click) the file MainPage.xaml in the Solution Explorer and click the Toolbox tab on the left to open the list of common XAML controls. Expand common XAML Controls and drag the required control to the middle of the design canvas.

After this, drag and drop the FlipView control. You have to change the name property. In Mainpage.Xaml, insert the code, given below, to FlipView tag-

  1. <FlipView.ItemTemplate>  
  2.     <DataTemplate>  
  3.         <Grid>  
  4.             <Image Source="{Binding}" Stretch="UniformToFill" /> </Grid>  
  5.     </DataTemplate>  
  6. </FlipView.ItemTemplate>  
  7. </FlipView>  
FlipView control

Step 4- Create a new folder in your project as Images and add some images to Images folder.

new folder

Step 5 - Add the Canvas control and change the name property.

Canvas control

Step 6- Add the button control, change the name and the content property.

 Button control

Add TextBlock control, change the name and text property.

Add TextBlock control

Step 7 - Double click the button control, which automatically creates click event method. 

method

Step 8- Add the code, given below, in constructor method (MainPage() method) in MainPage.xaml.cs. This code is used to set the image resource path for Flip View Control.

  1. String path = Directory.GetCurrentDirectory() + @
    "\Images";  
  2. FVtest.ItemsSource = Directory.GetFiles(path).Select(p => "ms-appx:///" + p);  
Now, the Constructor method looks like-  
  1. public MainPage()  
  2. {  
  3.     this.InitializeComponent();  
  4.     String path = Directory.GetCurrentDirectory() + @ "\Images";  
  5.     FVtest.ItemsSource = Directory.GetFiles(path).Select(p => "ms-appx:///" + p);  
  6. }  
Step 9- Add the code, given below, in btnSet_Click() method (MainPage() method) in MainPage.xaml.cs. The code, given below, is used to set the background image for Canvas control-
  1. private void btnSet_Click(object sender, RoutedEventArgs e)   
  2. {  
  3.     ImageBrush im = new ImageBrush();  
  4.     var ima = FVCan.SelectedItem;  
  5.     im.ImageSource = new BitmapImage(new Uri(ima.ToString(), UriKind.RelativeOrAbsolute));  
  6.     cantest.Background = im;  
  7. }  
code

Step 10 - Deploy your app in Local Machine and the output of the FVimgtoCanBG app is displayed.
 
Before clicking the SetImage button, the screenshot will look like-

SetImage button

After clicking the SetImage button, the screenshot will look like-
SetImage button

SetImage button

Summary

Now, you have successfully created and tested your canvas background as Flipview selected image, using FlipView Control and Canvas Control in Visual C# - UWP environment.