xamarin handle navigation in MVVM code example

Example 1: Navigate Handled in viewModel Xamarin

namespace YourApp.ViewModels
{
    public class CurrentPageViewModel
    {
        public ICommand BackToPage  {get; private set; }

        public CurrentPageViewModel()
        {
            BackToPage = new Command(async () => {
              await  Application.Current.MainPage.Navigation.PushModalAsync(new MainPage());
            });
        }
    }
}

Example 2: Navigate Handled in viewModel Xamarin

namespace YourApp.ViewModels
{
    public class MainPageViewModel 
    {
        public ICommand BackToMain { get; private set; }

        public MainPageViewModel()
        {
            BackToMain = new Command(async () => {
                await Application.Current.MainPage.Navigation.PopAsync();
            });
        }
    }
}

Tags:

Misc Example