Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Navigating to ViewModel Outside of MvxViewModel Context

0.00/5 (No votes)
9 Sep 2015 1  
This tip will explain how to perform a navigation to ViewModel outside MvxViewModel context on MvvmCross platform.

MvvmCross Navigation Using IMvxViewDispatcher

If you are familiar with mvvmcross platform, you probably know that there is nothing simpler then making a navigation from one viewmodel to another by simply calling ShowViewModel method that you get out of the box.

However, sometimes, you need to perform this kind of navigation outside the viewmodel.

Lucky us, we got an interface called IMvxViewDispatcher, which can be resolved using MvvmCross dependency injection.

Navigation

private void ShowViewModel<T>()
{
    var dispatcher = Mvx.Resolve<IMvxViewDispatcher>()
    dispatcher.ShowViewModel(new Cirrious.MvvmCross.ViewModels
                                           .MvxViewModelRequest(typeof(T), null, null, null));
}

Passing Parameters to ViewModel

You can pass parameters to viewmodel using MvxBundle object and receive them in InitFromBundle method.

//Creating parameters
Dictionary<string, string> bundle = new Dictionary<string, string>();
bundle.Add("some-key","some-value");

//Calling the method with bundle
ShowViewModel<SomeViewModelOfYours>(bundle);

                    
private void ShowViewModel<T>(object param = null)
{
    MvxBundle bundle = null;
    if (param != null)
    {
        //Creating MvxBundle from dictionary
        bundle = new MvxBundle(param.ToSimplePropertyDictionary());
    }

    var dispatcher = Mvx.Resolve<IMvxViewDispatcher>();
    dispatcher.ShowViewModel(new Cirrious.MvvmCross.ViewModels
                                      .MvxViewModelRequest(typeof(T), bundle, null,null));
}        

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here