MvvmCross - 遇到 ViewModel 注册问题
MvvmCross - Experiencing ViewModel registration issues
我有一个错误,我完全不知道为什么会这样。
ShellPage.cs:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
fragmentManager = FragmentManager;
RegisterFragments(bundle);
.
.
.
ViewModel.ShowMenu();
ViewModel.ShowFirstContent();
}
private void RegisterFragments(Bundle bundle)
{
RegisterFragment<MenuContent, MenuContentViewModel>(typeof(MenuContent).Name);
RegisterFragment<AddChildContent, AddChildContentViewModel>(typeof(AddChildContent).Name);
}
public bool Show(MvxViewModelRequest request, Bundle bundle)
{
.
.
.
if (request.ViewModelType == typeof(MenuContentViewModel))
{
ShowFragment(typeof(MenuContent).Name, Resource.Id.left_drawer, bundle);
return true;
}
else
{
ShowFragment(request.ViewModelType.Name, Resource.Id.content_frame, bundle);
return true;
}
}
ShellPageViewModel.cs
public class ShellPageViewModel : BaseViewModel
{
public void ShowMenu()
{
ShowViewModel<MenuContentViewModel>();
}
public void ShowFirstContent()
{
ShowViewModel<SelectChildContentViewModel>();
}
}
基本上,当调用 ShowFirstContent() 时,我得到以下错误:
Cirrious.CrossCore.Exceptions.MvxException: Could not find tag: SelectChildContentViewModel in cache, you need to register it first.
当从 OnCreate() 调用 RegisterFragment() 时,它不会引发任何错误,所以我假设它注册了片段和视图模型正确。
我是不是做错了什么?
我使用的代码全部基于 James Montemango 的代码:
https://github.com/jamesmontemagno/Xam.NavDrawer/tree/master/Material%20(Lollipop%20Style)/MvvmCross
其中的代码存在一些严重错误。我建议调查:https://github.com/MvvmCross/MvvmCross-AndroidSupport/tree/master/Samples
那里使用的代码是:
private void RegisterForDetailsRequests(Bundle bundle)
{
RegisterFragment<MenuFragment, MenuViewModel>(typeof(MenuViewModel).Name, bundle);
RegisterFragment<ExamplesFragment, ExamplesViewModel>(typeof(ExamplesViewModel).Name, bundle);
RegisterFragment<SettingsFragment, SettingsViewModel>(typeof(SettingsViewModel).Name, bundle);
}
public void RegisterFragment<TFragment, TViewModel>(string tag, Bundle args)
where TFragment : IMvxFragmentView
where TViewModel : IMvxViewModel
{
var customPresenter = Mvx.Resolve<IMvxFragmentsPresenter>();
customPresenter.RegisterViewModelAtHost<TViewModel>(this);
RegisterFragment<TFragment, TViewModel>(tag);
}
我有一个错误,我完全不知道为什么会这样。
ShellPage.cs:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
fragmentManager = FragmentManager;
RegisterFragments(bundle);
.
.
.
ViewModel.ShowMenu();
ViewModel.ShowFirstContent();
}
private void RegisterFragments(Bundle bundle)
{
RegisterFragment<MenuContent, MenuContentViewModel>(typeof(MenuContent).Name);
RegisterFragment<AddChildContent, AddChildContentViewModel>(typeof(AddChildContent).Name);
}
public bool Show(MvxViewModelRequest request, Bundle bundle)
{
.
.
.
if (request.ViewModelType == typeof(MenuContentViewModel))
{
ShowFragment(typeof(MenuContent).Name, Resource.Id.left_drawer, bundle);
return true;
}
else
{
ShowFragment(request.ViewModelType.Name, Resource.Id.content_frame, bundle);
return true;
}
}
ShellPageViewModel.cs
public class ShellPageViewModel : BaseViewModel
{
public void ShowMenu()
{
ShowViewModel<MenuContentViewModel>();
}
public void ShowFirstContent()
{
ShowViewModel<SelectChildContentViewModel>();
}
}
基本上,当调用 ShowFirstContent() 时,我得到以下错误:
Cirrious.CrossCore.Exceptions.MvxException: Could not find tag: SelectChildContentViewModel in cache, you need to register it first.
当从 OnCreate() 调用 RegisterFragment() 时,它不会引发任何错误,所以我假设它注册了片段和视图模型正确。
我是不是做错了什么?
我使用的代码全部基于 James Montemango 的代码:
https://github.com/jamesmontemagno/Xam.NavDrawer/tree/master/Material%20(Lollipop%20Style)/MvvmCross
其中的代码存在一些严重错误。我建议调查:https://github.com/MvvmCross/MvvmCross-AndroidSupport/tree/master/Samples
那里使用的代码是:
private void RegisterForDetailsRequests(Bundle bundle)
{
RegisterFragment<MenuFragment, MenuViewModel>(typeof(MenuViewModel).Name, bundle);
RegisterFragment<ExamplesFragment, ExamplesViewModel>(typeof(ExamplesViewModel).Name, bundle);
RegisterFragment<SettingsFragment, SettingsViewModel>(typeof(SettingsViewModel).Name, bundle);
}
public void RegisterFragment<TFragment, TViewModel>(string tag, Bundle args)
where TFragment : IMvxFragmentView
where TViewModel : IMvxViewModel
{
var customPresenter = Mvx.Resolve<IMvxFragmentsPresenter>();
customPresenter.RegisterViewModelAtHost<TViewModel>(this);
RegisterFragment<TFragment, TViewModel>(tag);
}