Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MvxCachingFragmentStatePagerAdapter和应用程序背景会导致ViewModel为空_C#_Xamarin_Xamarin.android_Mvvmcross - Fatal编程技术网

C# MvxCachingFragmentStatePagerAdapter和应用程序背景会导致ViewModel为空

C# MvxCachingFragmentStatePagerAdapter和应用程序背景会导致ViewModel为空,c#,xamarin,xamarin.android,mvvmcross,C#,Xamarin,Xamarin.android,Mvvmcross,复制步骤 在GitHub MvvmCross Samples/XPlatformMenus上使用示例/ 将应用程序部署到Android设备,限制后台进程最多设置为1 运行应用程序,从菜单中选择查看寻呼机示例 将应用程序发送到后台,加载其他应用程序,然后再次返回以确保其已被后台处理 预期行为 我希望视图寻呼机片段会重新出现在背景设置之前选择的选项卡上 实际行为 由于RecyclServicewFragment.OnCreateView中的以下代码,返回了空引用错误: _itemSelectedTo

复制步骤

在GitHub MvvmCross Samples/XPlatformMenus上使用示例/

将应用程序部署到Android设备,限制后台进程最多设置为1

运行应用程序,从菜单中选择查看寻呼机示例

将应用程序发送到后台,加载其他应用程序,然后再次返回以确保其已被后台处理

预期行为

我希望视图寻呼机片段会重新出现在背景设置之前选择的选项卡上

实际行为

由于RecyclServicewFragment.OnCreateView中的以下代码,返回了空引用错误:

_itemSelectedToken = ViewModel.WeakSubscribe(() => ViewModel.SelectedItem,
(sender, args) =>
{
    if (ViewModel.SelectedItem != null)
        Toast.MakeText(Activity,
        string.Format("Selected: {0}", ViewModel.SelectedItem.Title),
        ToastLength.Short).Show();
});
问题在于ViewModel尚未恢复,为空。

我遇到此问题的原因是我在自己的应用程序中使用MvxCachingFragmentStatePagerAdapter,而不是使用ViewModel类型初始化FragmentInfo:

new MvxCachingFragmentStatePagerAdapter.FragmentInfo("RecyclerView 1", typeof(RecyclerViewFragment), typeof(RecyclerViewModel))
我对每个片段使用相同的模型信息:

new MvxCachingFragmentStatePagerAdapter.FragmentInfo("RecyclerView 1", typeof(RecyclerViewFragment), ViewModel)
这会导致相同的行为-从后台恢复应用程序时,会出现空ViewModel

我正在使用MvvmCross 4.4和Android 7.0