首页 文章

Contract 失败 - Fragment标记为null

提问于
浏览
2

在一些Fragment导航中挣扎了很多,并且已经在这里进行了几天而没有任何进展 .

我有一个名为“NewsViewModel”的ViewModel,它包含属性NewsItemWraps . 这是包装器模型中的新闻列表 . 这只是在我的视图中显示在列表中,我希望用户能够标记新闻项并查看此新闻的详细信息 .

只要用户只看到相同的NewsDetail,这就完美无缺 . 但是,如果您选择第1项,导航回列表,选择项目2查看详细信息,然后再次导航回列表我收到以下错误: Contract failed - Fragment tag is null! Fragment tags are not set by default, you should add tag during FragmentTransaction or override UniqueImmutableCacheTag in your Fragment class.

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中将片段添加到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: Contract 失败 - Fragment标记为null!默认情况下不设置片段标记,您应该在FragmentTransaction期间添加标记或在Fragment类中覆盖UniqueImmutableCacheTag . 02-19 09:38:56.795 I / MonoDroid(30813):UNHANDLED EXCEPTION:02-19 09:38:56.795 I / MonoDroid(30813):System.InvalidOperationException: Contract 失败 - 片段标记为空!默认情况下不设置片段标记,您应该在FragmentTransaction期间添加标记或在Fragment类中覆盖UniqueImmutableCacheTag . 02-19 09:38:56.795 I / MonoDroid(30813):在MvvmCross.Droid.Support.V7.Fragging.Fragments.MvxFragmentExtensions.RegisterFragmentViewToCacheIfNeeded(IMvxFragmentView fragmentView)[0x0005a] in:0 02-19 09:38:56.795 I / MonoDroid(30813):在<filename unknown>中的MvvmCross.Droid.Support.V7.Fragging.Fragments.MvxBindingFragmentAdapter.HandleCreateCalled(System.Object sender,MvvmCross.Platform.Core.MvxValueEventArgs1 bundleArgs)[0x00024]:0 02-19 09:38:56.795 I / MonoDroid(30813):在VvvmCross.Platform.Core.MvxDelegateExtensionMethods.Raise [T](System.EventHandler1 eventHandler,System.Object sender,MvvmCross.Platform.Core.T value)[0x00000] in V :\ Xamarin \ MvvmCross \ MvvmCross \ Platform \ Platform \ Core \ MvxDelegateExtensionMethods.cs:21 02-19 09:38:56.795 I / MonoDroid(30813):at MvvmCross.Droid.Support.V7.Fragging.Fragments.EventSource.MvxEventSourceFragment .OnCreate(Android.OS.Bundle savedInstanceState)[0x00014] in:0 02-19 09:38:56.795 I / MonoDroid(30813):at Android.Support.V4.App.Fragment.n_OnC reate_Landroid_os_Bundle_(IntPtr jnienv,IntPtr native__this,IntPtr native_savedInstanceState)[0x00011] in:0 02-19 09:38:56.795 I / MonoDroid(30813):at(wrapper dynamic-method)System.Object:136761ed-d521-468a-9257 -5d08a4df776b(intptr,intptr,intptr)

1 回答

相关问题