2016-04-22 66 views
3

添加多個父活動我有一個片段需要被訪問,而用戶登錄並且當用戶是註銷。因此我需要一個片段用於兩個活動。我正在使用mvvmcross 4.1與Xamarin和Monodroid以及新添加的片段多親功能。我不確定我是否正確實施了。的一個片段

不希望使用ViewPager,因爲在查看屏幕時加載的相鄰屏幕。

當我嘗試導航到具有兩個父活動的片段時,出現Android.Content.ActivityNotFoundException: Unable to find explicit activity class {za.co.app.something.example/examplepcl.droid.views.GetHelpView}; have you declared this activity in your AndroidManifest.xml?錯誤。

我一直在尋找在GitHub上的示例項目:https://github.com/MvvmCross/MvvmCross-AndroidSupport/tree/master/Samples/Example.Droid

請發表評論,並告訴我,我是否應該與我的基類和的ViewModels的代碼更新我的問題。

我的片段:

using Android.OS; 
using Android.Runtime; 
using Android.Views; 
using Android.Widget; 
using MvvmCross.Binding.BindingContext; 
using ExamplePCL.Droid.ModalPopups; 
using ExamplePCL.ViewModels; 
using MvvmCross.Droid.Shared.Attributes; 

namespace ExamplePCL.Droid.Views 
{ 
    [MvxFragment(typeof(LoggedOutTabViewModel), Resource.Id.content_frame)] 
    [MvxFragment(typeof(LoggedInTabViewModel), Resource.Id.content_frame)] 
    [Register("examplepcl.droid.views.GetHelpView")] 
    public class GetHelpView : BaseFragment<GetHelpViewModel> 
    { 
     protected override int FragmentId => Resource.Layout.gethelp; 

     public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
     { 
      var view = base.OnCreateView(inflater, container, savedInstanceState); 

      return view; 
     } 
    } 
} 

我的活動:

活動1:

using Android.App; 
using Android.OS; 
using Android.Views; 
using ExamplePCL.ViewModels; 
using Android.Content.PM; 
using Android.Support.V4.Widget; 
using ExamplePCL.Droid.Fragments; 
using Android.Support.V4.View; 
using ExamplePCL.Droid.Interfaces; 
using MvvmCross.Droid.Support.V7.Fragging.Caching; 
using Android.Widget; 
using Android.Support.Design.Widget; 
using Android.Support.V4.Content; 

namespace ExamplePCL.Droid.Views 
{ 
    [Activity(
     Theme = "@style/AppTheme", 
     LaunchMode = LaunchMode.SingleTask, 
     ScreenOrientation = ScreenOrientation.Portrait, 
     WindowSoftInputMode = SoftInput.AdjustResize | SoftInput.StateAlwaysHidden, 
     Name = "examplepcl.droid.views.LoggedInTabView")] 
    public class LoggedInTabView : BaseLoggedCachingFragmentActivity<LoggedInTabViewModel>, IDrawerActivity 
    { 
     TabLayout _globalNavTabLayout; 
     int _previousTab; 

     #region Activity LifeCycle Handling 

     protected override int ContentLayoutId => Resource.Layout.loggedin_base_view; 

     protected override void OnCreate(Bundle bundle) 
     { 
      base.OnCreate(bundle); 

      if (bundle == null) 
       ViewModel.ShowMenu(); 

      // Setup tabs 
      _globalNavTabLayout = FindViewById<TabLayout>(Resource.Id.tabs); 
      SetupTabLayout(); 
     } 

     protected override void OnPause() 
     { 
      _globalNavTabLayout.TabReselected -= GlobalNavTabLayout_TabReselected; 
      _globalNavTabLayout.TabSelected -= GlobalNavTabLayout_TabSelected; 
      base.OnPause(); 
     } 

     protected override void OnResume() 
     { 
      _globalNavTabLayout.TabReselected += GlobalNavTabLayout_TabReselected; 
      _globalNavTabLayout.TabSelected += GlobalNavTabLayout_TabSelected; 
      base.OnResume(); 
     } 

     #endregion 

     #region Menu and Toolbar Option Handling 

     public override bool OnOptionsItemSelected(IMenuItem item) 
     { 
      switch (item.ItemId) 
      { 
       case Android.Resource.Id.Home: 
        DrawerLayout.OpenDrawer(GravityCompat.Start); 
        return true; 
      } 
      return base.OnOptionsItemSelected(item); 
     } 

     #endregion 

     #region Tab Layout 

     void GlobalNavTabLayout_TabSelected(object sender, TabLayout.TabSelectedEventArgs e) 
     { 
      int tabPosition = e.Tab.Position; 

      SetTabPosition(tabPosition); 
     } 

     void GlobalNavTabLayout_TabReselected(object sender, TabLayout.TabReselectedEventArgs e) 
     { 
      int tabPosition = e.Tab.Position; 

      SetTabPosition(tabPosition); 
     } 

     void SetupTabLayout() 
     { 
      _globalNavTabLayout.AddTab(_globalNavTabLayout.NewTab()); 
      _globalNavTabLayout.GetTabAt(0).SetCustomView(Resource.Layout.tab_layout_loggedin); 
      _globalNavTabLayout.GetTabAt(0).CustomView.FindViewById<ImageView>(Resource.Id.tab_icon).SetImageResource(Resource.Drawable.icon_my_bill); 

      _globalNavTabLayout.AddTab(_globalNavTabLayout.NewTab()); 
      _globalNavTabLayout.GetTabAt(1).SetCustomView(Resource.Layout.tab_layout_loggedin); 
      _globalNavTabLayout.GetTabAt(1).CustomView.FindViewById<ImageView>(Resource.Id.tab_icon).SetImageResource(Resource.Drawable.ico_my_products); 

      _globalNavTabLayout.AddTab(_globalNavTabLayout.NewTab()); 
      _globalNavTabLayout.GetTabAt(2).SetCustomView(Resource.Layout.tab_layout_loggedin); 
      _globalNavTabLayout.GetTabAt(2).CustomView.FindViewById<ImageView>(Resource.Id.tab_icon).SetImageResource(Resource.Drawable.btn_launchpadimage); 

      _globalNavTabLayout.AddTab(_globalNavTabLayout.NewTab()); 
      _globalNavTabLayout.GetTabAt(3).SetCustomView(Resource.Layout.tab_layout_loggedin); 
      _globalNavTabLayout.GetTabAt(3).CustomView.FindViewById<ImageView>(Resource.Id.tab_icon).SetImageResource(Resource.Drawable.ico_get_help); 

      _globalNavTabLayout.AddTab(_globalNavTabLayout.NewTab()); 
      _globalNavTabLayout.GetTabAt(4).SetCustomView(Resource.Layout.tab_layout_loggedin); 
      _globalNavTabLayout.GetTabAt(4).CustomView.FindViewById<ImageView>(Resource.Id.tab_icon).SetImageResource(Resource.Drawable.ico_track); 

      //Bring the TabLayout to the front so that it overlays the main content in the 
      var tabLayout = FindViewById<LinearLayout>(Resource.Id.linlay_appbar_globalnav); 
      tabLayout.BringToFront(); 

      SetTabPosition(2); 
     } 

     void SetTabPosition(int index) 
     { 
      switch (index) 
      { 
       case 0: 
        ViewModel.ShowMyBillScreenCommand.Execute(); 
        break; 
       case 1: // My Products 
        ViewModel.ShowMyProductsScreenCommand.Execute(); 
        break; 
       case 3: // Help 
        ViewModel.ShowGetHelpScreenCommand.Execute(); 
        break; 
       case 4: // Tracks 
        ViewModel.ShowTrackScreenCommand.Execute(); 
        break; 
       case 2: // QuickLinks 
        ViewModel.ShowQuickLinksScreenCommand.Execute(); 
        break; 
      } 
     } 

     #endregion 
    } 
} 

活動2:

using Android.App; 
using Android.OS; 
using Android.Content.PM; 
using ExamplePCL.ViewModels; 
using Android.Support.V4.View; 
using MvvmCross.Droid.Support.V7.Fragging.Caching; 
using ExamplePCL.Droid.Fragments; 
using Android.Support.Design.Widget; 
using Android.Widget; 
using Android.Views; 
using Android.Util; 
using Android.Content.Res; 
using Android.Support.V4.Content; 
using System.Threading.Tasks; 

namespace ExamplePCL.Droid.Views 
{ 
    [Activity(
     Theme = "@style/AppTheme", 
     LaunchMode = LaunchMode.SingleTask, 
     ScreenOrientation = ScreenOrientation.Portrait, 
     Name = "examplepcl.droid.views.LoggedOutTabView")] 
    public class LoggedOutTabView : BaseLoggedCachingFragmentActivity<LoggedOutTabViewModel> 
    { 
     TabLayout _globalNavTabLayout; 
     int _previousTab; 

     #region Activity LifeCycle Handling 

     protected override int ContentLayoutId => Resource.Layout.loggedout_base_view; 

     protected override void OnCreate(Bundle bundle) 
     { 
      base.OnCreate(bundle); 

      if (bundle == null) 
       ViewModel.ShowMenu(); 

      // Setup tabs 
      _globalNavTabLayout = FindViewById<TabLayout>(Resource.Id.tabs); 
      SetupTabLayout(); 
     } 

     protected override void OnPause() 
     { 
      _globalNavTabLayout.TabReselected -= GlobalNavTabLayout_TabReselected; 
      _globalNavTabLayout.TabSelected -= GlobalNavTabLayout_TabSelected; 

      base.OnPause(); 
     } 

     protected override void OnResume() 
     { 
      _globalNavTabLayout.TabReselected += GlobalNavTabLayout_TabReselected; 
      _globalNavTabLayout.TabSelected += GlobalNavTabLayout_TabSelected; 
      base.OnResume(); 
     } 

     #endregion 

     #region Tab Layout 

     void GlobalNavTabLayout_TabSelected(object sender, TabLayout.TabSelectedEventArgs e) 
     { 
      _globalNavTabLayout.TabSelected -= GlobalNavTabLayout_TabSelected; 
      _globalNavTabLayout.TabReselected -= GlobalNavTabLayout_TabReselected; 

      int tabPosition = e.Tab.Position; 

      SetTabPosition(tabPosition); 

      _globalNavTabLayout.TabSelected += GlobalNavTabLayout_TabSelected; 
      _globalNavTabLayout.TabReselected += GlobalNavTabLayout_TabReselected; 
     } 

     void GlobalNavTabLayout_TabReselected(object sender, TabLayout.TabReselectedEventArgs e) 
     { 
      _globalNavTabLayout.TabReselected -= GlobalNavTabLayout_TabReselected; 

      int tabPosition = e.Tab.Position; 

      SetTabPosition(tabPosition); 

      _globalNavTabLayout.TabReselected += GlobalNavTabLayout_TabReselected; 
     } 


     public void SetupTabLayout() 
     { 
      _globalNavTabLayout.AddTab(_globalNavTabLayout.NewTab()); 
      _globalNavTabLayout.GetTabAt(0).SetCustomView(Resource.Layout.tab_layout); 
      _globalNavTabLayout.GetTabAt(0).CustomView.FindViewById<ImageView>(Resource.Id.tab_icon).SetImageResource(Resource.Drawable.ico_get_help); 

      _globalNavTabLayout.AddTab(_globalNavTabLayout.NewTab()); 
      _globalNavTabLayout.GetTabAt(1).SetCustomView(Resource.Layout.tab_layout); 
      _globalNavTabLayout.GetTabAt(1).CustomView.FindViewById<ImageView>(Resource.Id.tab_icon).SetImageResource(Resource.Drawable.ico_track); 

      _globalNavTabLayout.AddTab(_globalNavTabLayout.NewTab()); 
      _globalNavTabLayout.GetTabAt(2).SetCustomView(Resource.Layout.tab_layout); 
      _globalNavTabLayout.GetTabAt(2).CustomView.FindViewById<ImageView>(Resource.Id.tab_icon).SetImageResource(Resource.Drawable.btn_launchpadimage); 

      _globalNavTabLayout.AddTab(_globalNavTabLayout.NewTab()); 
      _globalNavTabLayout.GetTabAt(3).SetCustomView(Resource.Layout.tab_layout); 
      _globalNavTabLayout.GetTabAt(3).CustomView.FindViewById<ImageView>(Resource.Id.tab_icon).SetImageResource(Resource.Drawable.ico_lock); 

      //Bring the TabLayout to the front so that it overlays the main content 
      var tabLayout = FindViewById<LinearLayout>(Resource.Id.linlay_appbar_globalnav); 
      tabLayout.BringToFront(); 

      SetTabPosition(2); 
     } 

     async void SetTabPosition(int index) 
     { 
      switch (index) 
      { 
       case 0: // Get Help 
        ViewModel.ShowGetHelpScreenCommand.Execute(); 
        break; 
       case 1: // Track 
        ViewModel.ShowTrackScreenCommand.Execute(); 
        break; 
       case 2: // Quick Links 
        ViewModel.ShowQuickLinksScreenCommand.Execute(); 
        break; 
       case 3: // Login 
        ViewModel.ShowLoginScreenCommand.Execute(); 
        break; 
      } 
     } 

     #endregion 
    } 
} 

UPDATE:

這個問題似乎是與我使用的庫MvvmCross.Droid.Shared.Attributes的方式。如果我只使用一個屬性([MvxFragment(typeof(LoggedOutTabViewModel), Resource.Id.content_frame)]),我仍然會得到相同的錯誤。

但是,如果我使用庫MvvmCross.Droid.Support.V7.Fragging.Attributes只有一個屬性,那麼我沒有得到任何錯誤。但顯然我不能使用該庫,因爲我需要兩個屬性來執行多個活動。

+0

在這種情況下,基於您登錄或不登錄這兩項活動,似乎很愚蠢。爲什麼不讓ViewModel根據它決定要做什麼,只需切換可用選項卡的可見性? – Cheesebaron

+0

謝謝你的回覆@cheesebaron !!我會喜歡這樣做,但我不能對視圖模型或架構做太多的修改,因爲iOS版本已經完成,並且android版本已接近英寸。我的控制做出了太多的架構改變:(所以我正在尋找一個解決方案,將離開我只需做最小的改變 –

+0

你的'IDrawerActivity'實現了什麼?這個'Interface'沒有在你的'LoggedOutTabView' – Pilatus

回答

1

嘗試從您的項目中刪除MvvmCross.Droid.Support.V7.Fragging包,因爲從Mvx 4.1.4版本開始,它一直是depricated。它現在形成MvvmCross.Droid.Shared包的一部分,您可以從 nuget Link下車。已經顯示安裝了這兩個軟件包會導致issues

+0

它花了很多重構,但刪除了包MvvmCross.Droid.Support.V7.Fraggin克'做了詭計。感謝您的幫助,您爲我節省了很多時間! –

+1

MvvmCross.Droid.Shared直到5.1.1才使用,需要更改爲MvvmCross.Droid.Support.Fragment – cfl

0

這更是一個解決辦法比的解決方案,但你可以這樣做:

而不必叫GetHelpView一個片段類,創建這三個 - >

GetHelpViewLoggedOut 

GetHelpViewLoggedIn 

GetHelpViewBase 

GetHelpViewBase可以包含所有常見代碼介於GetHelpViewLoggedInGetHelpViewLoggedOut之間,甚至重複使用相同的代碼ViewModel

不理想,但可能是一種解決方法。