我遇到了一个错误,我完全不知道为什么会出现这个错误。
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() 时,它不会引发任何错误,因此我假设它正确注册了片段和 View 模型。
我做错了什么吗?
我使用的代码全部基于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);
}
关于xamarin - MvvmCross - 遇到 ViewModel 注册问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32996733/