Search code examples
xamarinxamarin.formsxlabs

Xlabs - Pass a parameter to ViewModel


Is there a way to pass a parameter to a ViewModel using XLabs and NavigationService?

The 'args' parameter for the 'NavigateTo' method appears to be destined for the View, not the ViewModel and the 'Initialiser' method parameter on ViewFactory.Register does not seem to receive the arguments either.

Thanks,

Scott.


Solution

  • You can do like this:

    //MyOriginViewModel
    await Navigation.PushAsync<MyDestinyViewModel>((vm, v) => vm.Init(myVar), false);
    
    //MyDestinyViewModel
    public void Init(string myVar)
    {
        //
    }