简体   繁体   中英

How to intercept Navigation Bar Back Button Clicked Prism Xamarin Forms?

Basically Its the same question from Here but the solution dosen't work for Prism MVVM because the OnOptionsItemSelected(IMenuItem item) in the MainActivity never get raised. For the hardware button I'm using on that page:

protected override bool OnBackButtonPressed()
{
    return !PageUtilities.CanNavigate(this, null);
}

you can do like this

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
   e.Cancel = !PageUtilities.CanNavigate(this, null);
   base.OnBackKeyPress(e);
}

Cancel the event when navigation neeeds to be stopped

I guess there is no straight or single answer to this. Based on your requirement you must need to provide MVVM implementation.

Latest Prism Library update has the OnNavigatedFrom method (you'll require to override by implementing INavigationAware interface)

    public virtual void OnNavigatedFrom(NavigationParameters parameters)
    {
     // here is the place you would require to handle the Back button event, 
     // this is fired every-time, user tries to leave the view. 
    }

    public virtual void OnNavigatedTo(NavigationParameters parameters)
    {
     // fired upon view load
    }

You may require a static (boolean or something) variable to keep a check if the user is leaving the view and optionally display a message or invalidate the action.

Hope this helps.

Regards,

N Baua

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM