使用MvvmCross打开对话框片段时,将创建多个ViewModel实例

时间:2017-08-14 10:29:49

标签: android android-fragments xamarin mvvmcross android-dialogfragment

我最近开始使用MvvmCross 5并使用新的INavigationService。

但是,我一直无法找到任何新的方式来呈现Dialog Fragments,所以我仍然在自定义演示者(MvxFragmentsPresenter)中使用我的旧方法,如下所示:

protected override void ShowFragment(MvxViewModelRequest request)   
{

    var currentActivity = Mvx.Resolve<IMvxAndroidCurrentTopActivity>().Activity;
    var appActivity = currentActivity as MvvmCross.Droid.FullFragging.Views.MvxActivity;

                if (appActivity != null)
                {
                    var loaderService = Mvx.Resolve<IMvxViewModelLoader>();
                    var viewModel = loaderService.LoadViewModel(request, null);

                    if (request.ViewModelType == typeof(ExampleViewModel))
                    {
                        var dialogFragment = new ExampleDialogFragment();
                        dialogFragment.ViewModel = (ExampleViewModel)viewModel;
                        dialogFragment.SetStyle(DialogFragmentStyle.Normal, Resource.Style.MainDialogTheme);
                        dialogFragment.Show(appActivity.FragmentManager, ExampleDialogFragment.DialogTag);

                        return;
                    }
    }

我遇到的问题是当我需要将参数传递到此对话框片段时,导航服务会使用正确的参数创建视图模型,但是当它显示片段时,自定义演示者会构造一个新的查看没有任何参数的模型,并将其设置为新的对话框片段。

我尝试过使用MvxDefaultViewModelLocator而不是Loader,但这只是做同样的事情。

var locatorService = Mvx.Resolve<IMvxViewModelLocator>();
var viewModel = locatorService.Load(request.ViewModelType, new MvxBundle(request.ParameterValues), null);

是否有新方法打开对话框片段,以便可以为参数片段创建的参数模型分配给它?

感谢您的任何指示。

1 个答案:

答案 0 :(得分:0)

我们在https://github.com/MvvmCross/MvvmCross/pull/2099中添加了内置的Dialogs支持。这在Myget上可用,并将作为下一个MvvmCross版本的一部分发布。我们还在努力为多个viewmodel实例进行更永久的修复。