Back button
How to handle the back button visibility and pressed event
Last updated
public class CustomContentPage : MauiControls.ContentPage
{
public event EventHandler BackButtonPressed;
protected override bool OnBackButtonPressed()
{
BackButtonPressed?.Invoke(this, EventArgs.Empty);
return base.OnBackButtonPressed();
}
}[Scaffold(typeof(Project24.Controls.CustomContentPage))]
public partial class CustomContentPage
{
}
public class MainPage : Component
{
public override VisualNode Render()
{
return new NavigationPage()
{
new ContentPage()
{
new Button("Move To Page")
.VCenter()
.HCenter()
.OnClicked(OpenChildPage)
}
.Title("Main Page")
}
;
}
private async void OpenChildPage()
{
await Navigation.PushAsync<ChildPage>();
}
}
public class ChildPage : Component
{
public override VisualNode Render()
{
return new CustomContentPage()
{
new Button("Back")
.VCenter()
.HCenter()
.OnClicked(GoBack)
}
.Set(MauiControls.NavigationPage.HasBackButtonProperty, true)
.OnBackButtonPressed(OnBack)
.Title("Child Page");
}
async void OnBack()
{
await ContainerPage.DisplayAlert("Hey", "hey", "cancel");
}
private async void GoBack()
{
await Navigation.PopAsync();
}
}