locked
Xamarin + Prism: Pass parameters back RRS feed

  • Question

  • User136463 posted

    HI, I have a scenario where I use a NavigationPage as the main page and with Prism NavigateAsync navigate to a sub page. The user can navigate back by pressing back on the screen in the navigation bar. In other words, I don't use GoBackAsync because the navigation bar already allows the user to navigate back.

    How can I pass NavigationParameters back to the main page?

    I tried to override OnDisappearing or OnBackButtonPressed and use GoBackAsync there but then the sub page never opens and immediately navigates back to the main page. I am also not quiet clear about the usefulness of OnNavigatedFrom in the sub page. I would be nice if one could just add additional parameters to the parameters passed to the OnNavigatedFrom method of the sub page that would be passed to whatever page is then called.

    Any idea?

    Sunday, March 5, 2017 10:29 PM

Answers

  • User265422 posted

    Add "INavigationAware" to your viewmodel class like this

    public class AvailabilitiesViewModel : BindableBase, INavigationAware
    {
        // My viewmodel class content
    
        // INavigationAware
            public void OnNavigatedFrom(NavigationParameters parameters) 
            {
            // When you go out
            }
            public async void OnNavigatedTo(NavigationParameters parameters)
            {
            // When you go in
            }
    }
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, March 6, 2017 9:29 AM
  • User136463 posted

    I was not able to find any solution with passing parameters and have implemented a workaround using INotifyPropertyChanged. In the sub page my model implements the INotifyPropertyChanged interface and the main page ViewModel subscribes to these events and updates the page accordingly.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, March 6, 2017 9:29 AM
  • User116727 posted

    As @voidstream suggested, you can use the INavigationAware interface to pass parametrs back when using the hardware/software back buttons. Simply add the parameter to the parameters object in the OnNavigatedFrom method.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, March 6, 2017 3:07 PM

All replies

  • User265422 posted

    Add "INavigationAware" to your viewmodel class like this

    public class AvailabilitiesViewModel : BindableBase, INavigationAware
    {
        // My viewmodel class content
    
        // INavigationAware
            public void OnNavigatedFrom(NavigationParameters parameters) 
            {
            // When you go out
            }
            public async void OnNavigatedTo(NavigationParameters parameters)
            {
            // When you go in
            }
    }
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, March 6, 2017 9:29 AM
  • User136463 posted

    I was not able to find any solution with passing parameters and have implemented a workaround using INotifyPropertyChanged. In the sub page my model implements the INotifyPropertyChanged interface and the main page ViewModel subscribes to these events and updates the page accordingly.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, March 6, 2017 9:29 AM
  • User116727 posted

    As @voidstream suggested, you can use the INavigationAware interface to pass parametrs back when using the hardware/software back buttons. Simply add the parameter to the parameters object in the OnNavigatedFrom method.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, March 6, 2017 3:07 PM