How to navigate from one ContentPage to another in Xamarin.Forms

The following Demo Are For basic methods to navigate from one ContentPage to another.
Now Create New Solution.
The MainPage is initialized with a MainView, which we will define below, wrapped into a NavigationPage, which will create a navigation bar above the content.
Now Open The NavigationSegueXamarinForms.cs and Initialize NavigationBar.
1 2 3 4 |
public App () { MainPage = new NavigationPage (new MainView ()); } |
From Above Code We Are Redirect To the MainView and Add the Navigation Over That Page.
Now Open The MainView.cs File and Define the Button in that File. Below Code For Define Button.
1 2 3 4 |
var btn_PushAsync = new Button { Text = "PushAsync" }; |
Here We give The Button Name To ‘btn_PushAsync’ and Give That Button text to ‘PushAsync’.
Next We Perform The Click Event On btn_PushAsync Button.Below Code For Button Click Event.
1 2 3 4 |
btn_PushAsync.Clicked+= async (sender, e) => { await Navigation.PushAsync(new SecondPage()); }; |
When We Click On PushAsync Button It Will Redirect to SecondPage
Next We Put That Button To View
1 2 3 4 5 6 7 |
Content = new StackLayout { Children = { btn_PushAsyn } }; |
Now Create a New File ‘SecondPage’.So When user Click On PushAsync Button That Redirect to This SecondPage.
Now Open The SecondPage.cs File and Define More Button on That and Also Perform The Click Event of All Button. Below Code For That.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
public SecondPage () { var btn_PopAsync = new Button { Text = "PopAsync" }; var btn_PushModalAsync = new Button { Text = "PushModalAsync" }; var btn_PopModalAsync = new Button { Text = "PopModalAsync" }; var btn_PopToRootAsync = new Button { Text = "PopToRootAsync" }; btn_PopAsync.Clicked+= async (sender, e) => { await Navigation.PopAsync(); }; btn_PushModalAsync.Clicked+= async (sender, e) => { await Navigation.PushModalAsync(new MainView()); }; btn_PopModalAsync.Clicked+= async (sender, e) => { await Navigation.PopToRootAsync(); }; btn_PopToRootAsync.Clicked+= async (sender, e) => { await Navigation.PopToRootAsync(); }; Content = new StackLayout { Children = { btn_PopAsync, btn_PushModalAsync, btn_PopModalAsync, btn_PopToRootAsync } }; |
Now Our app is Ready to Run.Lets Run The App.
In Ios Device.
In Android Device.
If you like this tutorial then you can download full copy of the code from github.
Leave a Reply