使用 MvvmCross 打开对话框片段时创建 ViewModel 的多个实例
Multiple instances of ViewModel being created when opening a dialog fragment using MvvmCross
我最近开始使用 MvvmCross 5 和新的 INavigationService。
但是,我一直无法找到任何呈现对话框片段的新方法,因此我仍在自定义演示器 (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);
是否有打开对话框片段的新方法,以便可以将使用参数创建的视图模型分配给它?
感谢您的指点。
我们在 https://github.com/MvvmCross/MvvmCross/pull/2099 中添加了对对话框的内置支持,目前在 Myget 上可用,并将作为下一个 MvvmCross 版本的一部分发布。我们还在努力对多个视图模型实例进行更永久的修复。
我最近开始使用 MvvmCross 5 和新的 INavigationService。
但是,我一直无法找到任何呈现对话框片段的新方法,因此我仍在自定义演示器 (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);
是否有打开对话框片段的新方法,以便可以将使用参数创建的视图模型分配给它?
感谢您的指点。
我们在 https://github.com/MvvmCross/MvvmCross/pull/2099 中添加了对对话框的内置支持,目前在 Myget 上可用,并将作为下一个 MvvmCross 版本的一部分发布。我们还在努力对多个视图模型实例进行更永久的修复。