如何在使用Mvvmcross 5.x进行导航时接收捆绑包

时间:2018-02-02 21:02:05

标签: c# navigation mvvmcross

我是Android编程的全新手。 我选择了MvvmCross 5.x作为我的框架。 我想我理解如何发送一个'复杂'对象来自ViewModel的字典(包):

Task Navigate<TParameter>(IMvxViewModel<TParameter> viewModel,
                          TParameter param, 
                          IMvxBundle presentationBundle = null);

如何接收正在导航的ViewModel中的捆绑包?
也就是说,使用字典/包调用什么方法?

1 个答案:

答案 0 :(得分:0)

The presentationBundle parameter you used for navigation is part of the ViewModelRequest object, which is passed to the Show method of your MvxViewPresenter. You can create a custom implementation of a view presenter on your platform and use the PresentationBundle to customize for example how the navigation transition looks or perhaps to modify the navigation stack.

See the ViewModelRequest source code on GitHub. The property PresentationValues will contain the bundle you passed in the navigation. Following is an example implementation of custom view presenter on Android:

class CustomViewPresenter : MvxAndroidViewPresenter
{
    public CustomViewPresenter(IEnumerable<Assembly> androidViewAssemblies)
              : base(androidViewAssemblies)
    {
    }

    public override void Show(MvxViewModelRequest request)
    {
        if (request.PresentationValues.ContainsKey("something"))
        {
            //handle presentation value
        }
        base.Show(request);
    }
}

There are more methods you can override in the presenter that use ViewModelRequest and where you can then access the PresentationValues property. These are specific for each platform and depend on the type of navigation being performed. On Android you have ShowActivity, ShowDialogFragment, ShowFragment, etc. You can see the default implementation of MvxAndroidViewPresenter on MvvmCross Github as well.

You can see a nice example of how a custom ViewPresenter with PresentationBundle can be used in this blog post by the awesome Greg Shackles. He also has a nice introduction to view presenters.