PRISM ... calling async method in onNavigatedTo or any of the methods from INavigationAware

KyleWarrenKyleWarren ✭✭USUniversity ✭✭
edited March 2017 in Xamarin.Forms

I'm needing to call an async method when my pages are navigated to. I know making a void async is a no-no, but I really don't see a way around it.

So here is what I'm going to do and I was wondering if there is an async INavigationAware implementation that I'm missing/not finding.

    public void OnNavigatedTo(NavigationParameters parameters)
    {
        if (parameters.ContainsKey("title"))
            Title = (string)parameters["title"] + " and Prism";
    }

change to

    public async void OnNavigatedTo(NavigationParameters parameters)
    {
        if (parameters.ContainsKey("title"))
            Title = (string)parameters["title"] + " and Prism";

        await _processing.GetMenuOptions();
    }
Tagged:

Best Answer

Answers

  • DavidStrickland0DavidStrickland0 ✭✭✭ USMember ✭✭✭
    edited February 12

    So for anyone that comes across this and decides to try it. Changing OnNavigatedTo, OnNavigatingTo or OnNavigatedFrom to be async likely wont have the effect you would expect. Whether or not async void is acceptable here. If you DO override them and make them async so you can call some async method in them Prism isn't going to await your implementation. Instead it will call it and forget about it. This is exspecially obvious in OnNavigatingTo. If you change it to async and call an async method (like loading data) in all likely hood the page will be loaded and the UI updated before OnNavigatingTo finishes.

  • NMackayNMackay mod GBInsider, University mod

    @DavidStrickland0 said:
    So for anyone that comes across this and decides to try it. Changing OnNavigatedTo, OnNavigatingTo or OnNavigatedFrom to be async likely wont have the effect you would expect. Whether or not async void is acceptable here. If you DO override them and make them async so you can call some async method in them Prism isn't going to await your implementation. Instead it will call it and forget about it. This is exspecially obvious in OnNavigatingTo. If you change it to async and call an async method (like loading data) in all likely hood the page will be loaded and the UI updated before OnNavigatingTo finishes.

    Well you have to be careful how your async method behaves, I've done SQLite async fetches in OnNavigatingTo but you have have to be careful how you configure the await. the expected behavior shouldn't thread block anyway

    Mysettings = await _settingsService.GetUserSettings().ConfigureAwait(false);
    

    Mostly I set properties, nav params, bindings that will not thread block in OnNavigaingTo. You can't truly await OnAppearing as such in Forms, It's just an event to tell you the page has been loaded and pushed onto the navigation stack.

  • MatthewBelkMatthewBelk ✭✭ USMember ✭✭
    edited July 5

    Unfortunately, setting OnNavigatedTo or OnNavigatingTo to be async void means that you can't use the INavigationResult that's returned in the Task when you call INavigationService.NavigateAsync (in Prism 7) because the NavigateAsync returns instantly and the INavigationResult.Success property is always true. I am really struggling to figure out how to run async code in either of those 2 methods while still being about to take advantage of the INavigationResult.Success and .Exception properties. I've tried all manner of Task.Run() invocations using .Wait(), .ContinueWith(), etc. and I just can't get things to work as expected.

Sign In or Register to comment.