1

I have the following command inside a VM:

ICommand LaunchGameCommand => new Command(() =>
{
    //Navigation.PushAsync(...
});

According to the answers here I should be able to use something akin to the navigation in the commented code; however, the Navigation object seems to reside in Android.Content.Res... which seems to be something else entirely.

Is this still the correct method of navigating between views / viewmodels in Xamarin Forms, or has this now been superseded with an alternate method?

Paul Michaels
  • 16,185
  • 43
  • 146
  • 269

2 Answers2

1

Navigation is part of a page, you can’t find navigation property if you don’t have the reference to a some page, you need to have access to your current page in your view model to see this property, you can have access to your current page using

Application.Current.MainPage.Navigation.Push...

Ricardo Romo
  • 1,588
  • 12
  • 25
  • 1
    The issue I have with this is that if, from the VM, I need a reference to the page that I'm navigating to then I've broken the MVVM pattern. I can, obviously, use a method such as reflection to work around this... but every time I see any blurb about Xamarin Forms I'm told it's MVVM out of the box. – Paul Michaels Nov 05 '17 at 12:51
  • Yep, but using Application.Current. you have de current context of the application and you have access to your main or current page. – Ricardo Romo Nov 06 '17 at 20:36
1

Are you using any particular MVVM framework? Most of these include a way of navigating from VM to VM.

I use FreshMvvm. It allows you to perform the following to navigate between VMs and also pass data:

CoreMethods.PushPageModel<MyNextPageModel>(DataToPass);

More details here

Steve Chadbourne
  • 6,873
  • 3
  • 54
  • 82