我在一些Fragment导航方面遇到了一些困难,已经尝试了几天,但没有任何进展。
我有一个名为“NewsViewModel”的ViewModel,其中包含属性NewsItemWraps。这是新闻列表的包装器模型。这只是在我的视图中以列表的形式显示,并且我希望用户能够点击新闻项目并查看此新闻的详细信息。
只要用户只看到相同的NewsDetail,这就完全正常。但是,如果您选择项目1,在返回列表后,选择项目2查看详细信息,然后再次返回列表,我会收到以下错误: 合同失败-片段标记为空!默认情况下不设置片段标记,您应该在FragmentTransaction中添加标记或在Fragment类中重写UniqueImmutableCacheTag。
public override void OnBackPressed()
{
var currentFragment = SupportFragmentManager.FindFragmentById(Resource.Id.content_frame) as MvxFragment;
if (currentFragment != null && SupportFragmentManager.BackStackEntryCount >= 1)
{
SupportFragmentManager.PopBackStackImmediate(); //<-- This guy tosses the error
return;
}
if (DrawerLayout != null && DrawerLayout.IsDrawerOpen(GravityCompat.Start))
DrawerLayout.CloseDrawers();
else
base.OnBackPressed();
}
我试过在OnFragmentCreated中添加Fragment到BackStack时修改fragmentInfo标记,但这并没有改变任何东西。不管我是像在OnFragmentCreated中这样将其添加到backstack中,还是让'addToBackstack' bool像示例中一样处理,都没关系。它也不在乎它是否被缓存。
public override void OnFragmentCreated(IMvxCachedFragmentInfo fragmentInfo, Android.Support.V4.App.FragmentTransaction transaction)
{
// You can do fragment + transaction based configurations here.
// Note that, the cached fragment might be reused in another transaction afterwards.
//Adding the fragment to the stack manually.
if (fragmentInfo.Tag.Contains(typeof(NewsDetailsViewModel).Name))
{
transaction.AddToBackStack(typeof(NewsDetailsViewModel).Name);
}
base.OnFragmentCreated(fragmentInfo, transaction);
var myCustomInfo = fragmentInfo as CustomFragmentInfo; //How the sample handles the backstack in the factory MainActivityFragmentCacheInfoFactory.
}
我的类型在cacheFactory中:
{
typeof (NewsDetailsViewModel).ToString(),
new CustomFragmentInfo(typeof (NewsDetailsViewModel).Name,
typeof (NewsDetailsFragment),
typeof (NewsDetailsViewModel), cacheFragment: false, addToBackstack: true, isRoot:false )
}
在 NewsViewModel 中的属性:
private List<NewsItemWrap> _newsItemWraps;
public List<NewsItemWrap> NewsItemWraps
{
get { return _newsItemWraps ?? (_newsItemWraps = new List<NewsItemWrap>()); }
set { _newsItemWraps = value; RaisePropertyChanged(() => NewsItemWraps); }
}
我一直在玩和修改Mvvmcross的示例,链接如下:https://github.com/MvvmCross/MvvmCross-AndroidSupport/tree/master/Samples
非常感谢任何指针和建议。
完整的堆栈:
System.InvalidOperationException:合同失败 - 片段标记为null!默认情况下不设置片段标记,您应该在FragmentTransaction中添加标记或在Fragment类中重写UniqueImmutableCacheTag。
02-19 09:38:56.795 I / MonoDroid(30813):未处理的异常:02-19 09:38:56.795 I / MonoDroid(30813):System.InvalidOperationException:合同失败 - 片段标记为null!默认情况下不设置片段标记,您应该在FragmentTransaction中添加标记或在Fragment类中重写UniqueImmutableCacheTag。 02-19 09:38:56.795 I / MonoDroid(30813): 在MvvmCross.Droid.Support.V7.Fragging.Fragments.MvxFragmentExtensions.RegisterFragmentViewToCacheIfNeeded(IMvxFragmentView fragmentView)[0x0005a]中:0 02-19 09:38:56.795 I / MonoDroid(30813): 在MvvmCross.Droid.Support.V7.Fragging.Fragments.MvxBindingFragmentAdapter.HandleCreateCalled(System.Object sender,MvvmCross.Platform.Core.MvxValueEventArgs<code>1 bundleArgs)[0x00024]中:在<filename unknown>:0 02-19 09:38:56.795 I / MonoDroid(30813): 在MvvmCross.Platform.Core.MvxDelegateExtensionMethods.Raise[T](System.EventHandler<code>1 eventHandler,System.Object sender,MvvmCross.Platform.Core.T value)[0x00000]中:在V:\ Xamarin \ MvvmCross \ MvvmCross \ Platform \ Platform \ Core \ MvxDelegateExtensionMethods.cs:21 02-19 09:38:56.795 I / MonoDroid(30813): 在MvvmCross.Droid.Support.V7.Fragging.Fragments.EventSource.MvxEventSourceFragment.OnCreate(Android.OS.Bundle savedInstanceState)[0x00014]中:0 02-19 09:38:56.795 I / MonoDroid(30813): 在Android.Support.V4.App.Fragment.n_OnCreate_Landroid_os_Bundle_(IntPtr jnienv,IntPtr native__this,IntPtr native_savedInstanceState)[0x00011]中:在:0 02-19 09:38:56.795 I / MonoDroid(30813): 在(包装器dynamic-method)System.Object:136761ed-d521-468a-9257-5d08a4df776b(intptr,intptr,intptr)