Thomas Carlton
Thomas Carlton

Reputation: 5968

How to handle backbutton on .Net Maui?

I need to handle the back button action in .Net Maui.

I'm not talking about the device backbutton. But the navigation page back button :

enter image description here

I tried this code but it works only on the device back button and not navigation page back button.

protected override bool OnBackButtonPressed()
{        
    // Do something here 
    return base.OnBackButtonPressed();
}

How can I handle that?

Upvotes: 7

Views: 16218

Answers (3)

Shrikant Verma
Shrikant Verma

Reputation: 839

Use the below code to handle the back button in MAUI app and the same code will work for WebView as well.


protected override bool OnBackButtonPressed()
    {
        if (WebView.CanGoBack)
        {
            WebView.GoBack();
            return true;
        }
        else
        {
            base.OnBackButtonPressed();
            return false;
        }
    }

Upvotes: 9

Guangyu Bai - MSFT
Guangyu Bai - MSFT

Reputation: 4586

You can set the Shell Navigation back button by using the back button behavior.

  • Command, of type ICommand, which is executed when the back button is pressed.
  • CommandParameter, of type object, which is the parameter that's passed to the Command.

Here is the information about Back button behavior.

Upvotes: 4

rentoulis
rentoulis

Reputation: 414

I think that this will help: "Back button appearance and behavior can be redefined by setting the BackButtonBehavior attached property to a BackButtonBehavior object", according to MAUI documentation.

Using "Command" property you can define the actions to executed when back-button is pressed.

Upvotes: 7

Related Questions