Let's create a modal page
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="pav.StackNavigation.Views.ModalPage">
<ContentPage.Content>
<StackLayout>
<Label Text="This is a modal page"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
<Button Text="back"
x:Name="Back_Button"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
<Button Text="Modal"
x:Name="Modal_Button"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace pav.StackNavigation.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ModalPage : ContentPage {
public ModalPage () {
InitializeComponent ();
Back_Button.Clicked += async (s,e)=> await this.Navigation.PopModalAsync();
Modal_Button.Clicked += async (s, e) => await this.Navigation.PushModalAsync(new ModalPage());
}
}
}
if you take a look at the modal page you'll notice that that normal header back button is not present
however most devices have a dedicated back button which will pop the modal off the modal stack just the way it should, to block this behavior you can override the OnBackButtonPressed function.
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace pav.StackNavigation.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ModalPage : ContentPage {
public ModalPage () {
InitializeComponent ();
Back_Button.Clicked += async (s,e)=> await this.Navigation.PopModalAsync();
Modal_Button.Clicked += async (s, e) => await this.Navigation.PushModalAsync(new ModalPage());
Second_Button.Clicked += async (s, e) => await this.Navigation.PushAsync(new SecondPage());
}
protected override bool OnBackButtonPressed() {
return true; // block back button
return false; // pop current modal
}
}
}
now if you try to push to the normal stack while on a Modal stack you'll get an exception
which is probably a good thing since navigating to a normal page from a modal would be ridiculous.