2016-02-19 65 views
2

用一些片段導航掙扎了很多,並且在這幾天沒有任何進展。合同失敗 - 片段標籤爲空

我有一個名爲「NewsViewModel」,其中包含屬性NewsItemWraps的ViewModel。這是包裝模型中的新聞列表。這只是在我的視圖中顯示在列表中,我希望用戶能夠選擇一個新聞項目並查看此新聞的詳細信息。

只要用戶只能看到相同的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(); 
    } 

我已經試過修改的fragmentInfo標籤時,我的片段添加到堆棧中的OnFragmentCreated,但這並沒有改變任何東西。沒關係,如果我像OnFragmentCreated中那樣將它添加到後臺堆棧中,或者讓'addToBackstack'布爾處理,就像在示例中一樣。它也不關心它是否被緩存。

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.MvxValueEventArgs 1 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)

+0

我見過這種情況經常發生。這可能是4.0中的一個錯誤。你能回答這個問題嗎? – Martijn00

+0

當然沒有問題:) – Stupidus

回答