gpt4 book ai didi

android - 契约(Contract)失败 - fragment 标签为空

转载 作者:可可西里 更新时间:2023-11-01 11:42:55 24 4
gpt4 key购买 nike

在一些 Fragment 导航上遇到了很多困难,并且已经进行了几天,现在没有任何进展。

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

只要用户只看到相同的 NewsDetail,它就可以正常工作。但是,如果您选择第 1 项,导航回列表,选择第 2 项查看详细信息,然后再次导航回列表,我会收到以下错误:契约(Contract)失败 - fragment 标签为空!默认情况下不设置 fragment 标签,您应该在 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 中那样将它添加到后台堆栈,还是像示例中那样让“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 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.

02-19 09:38:56.795 I/MonoDroid(30813): UNHANDLED EXCEPTION: 02-19 09:38:56.795 I/MonoDroid(30813): System.InvalidOperationException: 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. 02-19 09:38:56.795 I/MonoDroid(30813): at MvvmCross.Droid.Support.V7.Fragging.Fragments.MvxFragmentExtensions.RegisterFragmentViewToCacheIfNeeded (IMvxFragmentView fragmentView) [0x0005a] in :0 02-19 09:38:56.795 I/MonoDroid(30813): at MvvmCross.Droid.Support.V7.Fragging.Fragments.MvxBindingFragmentAdapter.HandleCreateCalled (System.Object sender, MvvmCross.Platform.Core.MvxValueEventArgs1
bundleArgs) [0x00024] in <filename unknown>:0 02-19 09:38:56.795
I/MonoDroid(30813): at
MvvmCross.Platform.Core.MvxDelegateExtensionMethods.Raise[T]
(System.EventHandler
1 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_OnCreate_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)

最佳答案

这是当前 Nuget 中的一个错误,并已在此提交中修复:https://github.com/MvvmCross/MvvmCross-AndroidSupport/commit/72356791714795c9d711a0c491367fd5c5ebdb82

我们将很快发布更新。

关于android - 契约(Contract)失败 - fragment 标签为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35501200/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com