Image Navigatiion In Xamarin.Forms Application

Introduction 
 
This article demonstrates image navigation in Xamarin.Forms applications.
 
Let's start.
 
Step 1 
 
Open Visual Studio and go to New Project >> Installed >> Visual C# >> Cross-Platform.
 
Select Cross-platform app, then give your project a name and location.
 
 
 
Step 2 
 
Next, add an image to Solution Explorer >> Project Name.Android >> Resources >> Right-Click >> drawable >> Add >> Existing Item.
 
 
 
Next, a dialogue box will open. Choose image location and add images.
 
 
 
The image is added successfully.
 
 
Step 3 
 
Open Solution Explorer >> Project Name (Portable) >> App.Xaml.cs. Open the design view of this page.
 
 
 
The code is given below.
 
 
 
C# Code  
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. using Xamarin.Forms;  
  7.   
  8. namespace Image_Navigation  
  9. {  
  10.     public partial class App : Application  
  11.     {  
  12.         public App()  
  13.         {  
  14.             InitializeComponent();  
  15.   
  16.             MainPage = new NavigationPage (new MainPage());  
  17.         }   
Step 4
 
Open Solution Explorer >> Project Name (Portable) >> MainPage.xaml. Open the design view of this page.
 
 
 
The code is given below.
 
 
 
XAML Code 
 
We are creating the image inside the StackLayout and adding an image name copied from the source. Image Name is "" Untitled.jpg".
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              xmlns:local="clr-namespace:Image_Navigation"  
  5.              x:Class="Image_Navigation.MainPage">  
  6.     <ContentPage.Content>  
  7.         <StackLayout>  
  8.             <Image Source="Untitled.jpg"   
  9.                    VerticalOptions="Center"  
  10.                    HorizontalOptions="Center">  
  11.                 <Image.GestureRecognizers>  
  12.                     <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"/>  
  13.                 </Image.GestureRecognizers>  
  14.             </Image>  
  15.         </StackLayout>  
  16.     </ContentPage.Content>  
  17. </ContentPage>   
Step 5
 
Open Solution Explorer >> Project Name (Portable) >> Right-Click >> Add >> New Item.
 
 
 
A new dialogue box will open. Now, add the XAML page and give it a name. Click the "Add" button.
 
 
 
Step 4 
 
Open Solution Explorer >> Project Name >> Page1.xaml. Open the Design View of this page.
 
 
 
The code is given below.
 
 
 
XAML Code 
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              x:Class="Image_Navigation.Page1">  
  5.     <ContentPage.Content>  
  6.         <StackLayout>  
  7.             <Image Source="download.jpg"  
  8.                     VerticalOptions="Center"  
  9.                    HorizontalOptions="Center"/>  
  10.         </StackLayout>  
  11.     </ContentPage.Content>  
  12. </ContentPage>  
  13.       
  14.       
Step 5 
 
Open Solution Explorer >> Project Name (portable) >> MainPage.xaml.cs. Open the design view of this page.
 
 
 
The code is given below.
 
 
 
C# Code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Xamarin.Forms;  
  7.   
  8. namespace Image_Navigation  
  9. {  
  10.     public partial class MainPage : ContentPage  
  11.     {  
  12.         public MainPage()  
  13.         {  
  14.             InitializeComponent();  
  15.         }  
  16.   
  17.         private async void TapGestureRecognizer_Tapped(object sender, EventArgs e)  
  18.         {  
  19.             await Navigation.PushAsync(new Page1());  
  20.         }  
  21.     }  
  22. }  
Step 6 
 
Next, select the Build & deploy option, followed by selecting from the list of Android Emulator or Simulator. You can choose any API (Android Program Interface) Level Emulator or simulator to run it.
 
Output 
 
After a few seconds, you will see your app working.
 
Click the Image Navigation and the second page is displayed.
 

Finally, we have successfully created Xamarin.Forms Image Navigation.


Similar Articles