NavigationService for Xamarin.Forms MVVM :Wait() vs await

390 Views Asked by At

I was working on a simpleNavigation Service to learn the basic principles and in fact it was very helpful to understand this concept. When I was making trial and error to incorporate it to my application, I used the NavigateModalAsync() of NavigationService. In order to call this method, I used a command interface of a button. It worked pretty well. For the sake of simplicity I didnt show this command and its binding.It worked pretty well with:

 ....
    public async Task NavigateAsync(string pageKey, object parameter, bool animated = true)
    {
        var page = GetPage(pageKey, parameter);
        await CurrentNavigationPage.Navigation.PushAsync(page, animated);
    }
 .....
    private async void OpenExamplesOfDefPageAsync() //It worked very well
    {
        await App.NavigationService.NavigateModalAsync("ExamplesOfDefPage");
    }
 ....

However, as my primary intention was to navigate to another page when an item of listview is Selected, and since there is no any command interface is available for Listview.ItemSelected, I tried to call NavigateModalAsync() method by binding Listview.SelectedItem to MVVM. I thought the best place to call NavigateModalAsync() is the Set of SelectedItem property just after OnPropertyChanged is called. But since it is not allowed to use await here, I thought to use .Wait() method to await the task. However, the application freezes/Crashes when the debugger comes to that statement.

    .....
    public object SelectedItem
    {
        get { return selectedItem; }
        set
        {
            if (selectedItem != value)
            {
                selectedItem = value;
                OnPropertyChanged("SelectedItem");
                App.NavigationService.NavigateModalAsync("ExamplesOfDefPage").Wait();
            }
        }
    }
    ....

Actually I solved this by a method which calls NavigateModalAsync. So my question what is the difference between await and.Wait() that it works with await but dont work with Wait()

0

There are 0 best solutions below