How To Apply ToolbarItems Navigation In Xamarin.Forms Application

Introduction
 
This article demonstrates how to apply ToolBarItems navigation to Xamarin.Forms.
 
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 
 
Update the following NuGet Packages to your project.
  • Xamarin.Forms 
 Go to Solution Explorer and select your solution. Right click and select "Manage NuGet Packages for Solution".
 
 
 
Now, select the following NuGet packages and select your project to update this version.
 
 
 
Step 3 
 
Next, add an image to Solution Explorer >> Project Name.Android >> Resources >> Right-click >> drawable.
 
 
 
Next, a dialogue box will open. Choose image location and add images.
 
 
 
The image is added successfully.
 
 
 
Step 4 
 
Open Solution Explorer >> Project Name >> Mainpage.xaml. Open the Design View of this page.
 
 
 
The code is given below.
 
We are going to create a ToolbarItem Text and Icon on the Content page. For, ToolbarItems and image name, copy and paste an icon "Icon=download.png".
 
 
 
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.              xmlns:local="clr-namespace:ToolbarItems"  
  5.              x:Class="ToolbarItems.MainPage">  
  6.   
  7.     <ContentPage.ToolbarItems>  
  8.         <ToolbarItem Text="New"  
  9.                      Icon="download.png"  
  10.                      Priority="0"  
  11.                      Order="Primary"  
  12.                      Clicked="ToolbarItem_Clicked"/>  
  13.          
  14.     </ContentPage.ToolbarItems>  
  15.     <StackLayout>  
  16.         <Label Text="ToolbarItem"  
  17.                FontSize="50"  
  18.                HorizontalOptions="Center"  
  19.                VerticalOptions="Center"  
  20.                TextColor="YellowGreen"/>  
  21.         <Label x:Name="MyLabel"  
  22.                FontSize="70"  
  23.                HorizontalOptions="Center"  
  24.                VerticalOptions="Center"  
  25.                TextColor="Red"/>  
  26.     </StackLayout>  
  27. </ContentPage>   
Step 5 
 
Open Solution Explorer >> Project Name >> Right-click >> Add >> New Item. 
 
 
 
The Dialogue box will open. Now, add the XAML page and give your own name and click the "ADD" button.
 
 
 
Step 6 
 
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="ToolbarItems.Page1"  
  5.              Title="Page1">  
  6.     <ContentPage.Content>  
  7.         <StackLayout>  
  8.             <Label Text="ToolbarItemNavigation"  
  9.                    FontSize="50"  
  10.                    TextColor="Red"  
  11.                 VerticalOptions="CenterAndExpand"   
  12.                 HorizontalOptions="CenterAndExpand" />  
  13.         </StackLayout>  
  14.     </ContentPage.Content>  
  15. </ContentPage>  
Step 7 
 
Next, open Solution Explorer >> Project Name >> 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 ToolbarItems  
  9. {  
  10.     public partial class MainPage : ContentPage  
  11.     {  
  12.         public MainPage()  
  13.         {  
  14.             InitializeComponent();  
  15.         }  
  16.   
  17.          
  18.         async private void ToolbarItem_Clicked(object sender, EventArgs e)  
  19.         {  
  20.             await Navigation.PushAsync(new Page1());  
  21.         }  
  22.     }  
  23. }  
Step 8 
 
Open Solution Explorer >> Project Name >> 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 ToolbarItems  
  9. {  
  10.     public partial class App : Application  
  11.     {  
  12.         public App ()  
  13.         {  
  14.             InitializeComponent();  
  15.   
  16.             MainPage = new NavigationPage(new MainPage());  
  17.         }  
Step 9 
 
Next, select the build & deploy option, followed by selecting from the list of Android Emulators or simulator. You can choose any API (Application Program Interface) Level Emulator and simulator to run it.
 
Output 
 
After a few seconds, you will see your app working.
 
Click on the ToolbarItems Icon Navigation page in page1. 
 
 
 
Finally, we have successfully created Xamarin.Forms ToolbarItems Navigation.


Similar Articles