Hello,
You can bind a Command to Back Button in Shell, then execute some c# code in your ViewModel's command event.
For example, I have a detailed page, I add Shell.BackButtonBehavior
and bind a BackButtonCommand
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="App2.Views.ItemDetailPage"
Title="{Binding Title}">
<Shell.BackButtonBehavior>
<BackButtonBehavior Command="{Binding BackButtonCommand}"/>
</Shell.BackButtonBehavior>
We can execute some C# code in the ViewModel
when click the back button.
public ICommand BackButtonCommand { get; set; }
public ItemDetailViewModel()
{
BackButtonCommand = new Command(async () => {
//Execute your code when click the back button in the shell
//navigate to the previous page
await Shell.Current.GoToAsync("..");
});
}
Best Regards,
Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.