MVVM交叉异常。无法创建碎片。当使用Android支持碎片时,请使用MvxAppCompatViewPresenter。

问题描述 投票:0回答:1

我在让我的碎片运行时遇到了麻烦,并且得到了以下异常。MVVM交叉异常。Cannot Create Fragment. 当使用Android支持Fragments时,请使用MvxAppCompatViewPresenter。

这里是MainViewModel,它应该导航到第一个fragmentViewModel(ActionViewModel)。

    public class MainViewModel : BaseViewModel
    {
        public IMvxCommand<string> BottomNavigationItemSelectedCommand { get; private set; }

        List<TabViewModel> _tabs;
        public List<TabViewModel> Tabs
        {
            get => _tabs;
            set => SetProperty(ref _tabs, value);
        }

        private readonly IMvxNavigationService _navigationService;

        public MainViewModel(IMvxNavigationService navigationService)
        {
            _navigationService = navigationService;
            BottomNavigationItemSelectedCommand = new MvxCommand<string>(BottomNavigationItemSelected);

            var tabs = new List<TabViewModel>
            {
                Mvx.IoCProvider.IoCConstruct<ActionViewModel>(),
                Mvx.IoCProvider.IoCConstruct<TaskViewModel>(),
                Mvx.IoCProvider.IoCConstruct<KpiViewModel>(),
                Mvx.IoCProvider.IoCConstruct<EisViewModel>(),
                Mvx.IoCProvider.IoCConstruct<MenuViewModel>()
            };

            Tabs = tabs;
        }

        public override Task Initialize()
        {
            return base.Initialize();
        }


        // Android-only, not used on iOS
        private void BottomNavigationItemSelected(string tabId)
        {
            if (tabId == null)
            {
                return;
            }

            foreach (var item in Tabs)
            {
                if (tabId == item.TabId)
                {
                    _navigationService.Navigate(item);
                    break;
                }
            }
        }
    }
}

而这是ActionFragment。

    [MvxFragmentPresentation(typeof(MainViewModel), Resource.Id.fragment_content, true)]
    [Register(nameof(ActionFragment))]
    public class ActionFragment : BaseFragment<ActionViewModel>
    {
        //public static ActionFragment NewInstance() => new ActionFragment();

        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
        }

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            var view = inflater.Inflate(Resource.Layout.activity_actions, container, false);
            return view;
        }

    }

这是主要的活动布局。

<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:id="@+id/parentView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?android:attr/colorBackground">

    <androidx.appcompat.widget.LinearLayoutCompat
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <androidx.appcompat.widget.ContentFrameLayout
            android:id="@+id/fragment_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"/>

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:labelVisibilityMode="labeled"
            app:menu="@menu/bottom_nav_menu"
            local:MvxBind="BottomNavigationSelectedBindingKey BottomNavigationItemSelectedCommand"/>

    </androidx.appcompat.widget.LinearLayoutCompat>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

谁能告诉我,我应该怎么做才能解决这个问题?我是新手

android-fragments xamarin.android mvvmcross bottomnavigationview
1个回答
0
投票

我发现我的问题是我必须改变MainApplication.cs和Setup.cs类。

从.NET类改成了.NET类。

public class Setup : MvxAndroidSetup

public class Setup : MvxAppCompatSetup<App>

并从 。

public class MainApplication : MvxAndroidApplication<MvxAndroidSetup<App>, App>

public class MainApplication : MvxAppCompatApplication<Setup, App>
© www.soinside.com 2019 - 2024. All rights reserved.