为一个片段添加多个父活动

时间:2016-04-22 10:10:57

标签: c# android xamarin xamarin.android mvvmcross

我有一个片段,当用户登录且用户已注销时,需要访问该片段。因此,两个活动需要使用一个片段。我正在使用 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
    }
}

更新

问题似乎与我使用库MvvmCross.Droid.Shared.Attributes的方式有关。如果我只使用一个属性([MvxFragment(typeof(LoggedOutTabViewModel), Resource.Id.content_frame)]),我仍会得到同样的错误。

但是,如果我只使用一个属性库MvvmCross.Droid.Support.V7.Fragging.Attributes,那么我没有错误。但显然我无法使用该库,因为我需要两个属性来进行多项活动。

2 个答案:

答案 0 :(得分:2)

尝试从项目中删除MvvmCross.Droid.Support.V7.Fragging包,因为从Mvx 4.1.4版本开始depricated。它现在构成了MvvmCross.Droid.Shared包的一部分,您可以将其从 nuget Link下来。安装了这两个软件包后,显示会导致issues

答案 1 :(得分:0)

这更像是一种解决方法,而不是解决方案,但您可以这样做:

不要让一个名为GetHelpView的Fragment类创建这三个 - &gt;

GetHelpViewLoggedOut

GetHelpViewLoggedIn

GetHelpViewBase

GetHelpViewBase可以包含GetHelpViewLoggedInGetHelpViewLoggedOut之间的所有公共代码,甚至可以重用相同的ViewModel

不理想,但可能是一种解决方法。