DarthMazut / MochaLibrary

Just another MVVM library which gives you some abstract classes out-of-the-box.
0 stars 0 forks source link

Should OnNavigat* methods be events? #12

Open DarthMazut opened 1 year ago

DarthMazut commented 1 year ago

Right now OnNavigat* methods are provided by corresponding interfaces. This means NavigationService can await asynchronous versions of these methods (and actually is doing that at this point). That soultion has some pros and cons.

Example No.1: Client wants to start downloading something after navigating to some page. What is the natural way to do that? Well isn't that just:

public Task OnNavigatedToAsync(OnNavigatedToEventArgs e)
{
    await DownloadStuff();
    ChangeSomeUiStuff();
}

The problem with that is NavigationService is awaiting that, and the navigation process is not getting finished before DownloadStuff() finishes. Obviously the navigation should have ended by that time. Of course this can be fixed by implementing synchronouse version of OnNavigatedTo and just mark void as async. There you have it - event like behaviour. But it seems quite counter intuitive to use async void when you have OnNavigatedToAsync ...

Example No.2: Quite common scenario: before navigating from current page we want to prompt user whether he/she wants to save changes... So here it's very simple:

public Task OnNavigatingFromAsync(OnNavigatingFromEventArgs e)
{
    bool proceed = await myPrompt.ShowModalAsync();
    if (proceed == false)
    {
        e.Cancel = true;
    }
}

In case, OnNavigatingFrom was an event we could not prevent navigation while awaiting prompt, navigation would probably already completed.

So we could like e.Cancel = true is sync part, and then make navigation request again... (?)