MVVM 交叉异常:无法创建片段。使用 Android 支持片段时使用 MvxAppCompatViewPresenter
MVVM Cross Exception: Cannot Create Fragment. Use the MvxAppCompatViewPresenter when using Android Support Fragments
我无法将我的片段传送到 运行,我收到以下异常。 MVVM 交叉异常:无法创建片段。使用 Android 支持片段时使用 MvxAppCompatViewPresenter。
这是 MainViewModel,它应该导航到第一个 fragment/ViewModel (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;
}
}
这是主要的 activity 布局:
<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>
有人可以让我知道我应该怎么做才能解决这个问题吗?我是新手!
我发现我的问题是我必须更改 MainApplication.cs 和 Setup.cs 类。
来自 :
public class Setup : MvxAndroidSetup
到
public class Setup : MvxAppCompatSetup<App>
来自:
public class MainApplication : MvxAndroidApplication<MvxAndroidSetup<App>, App>
到
public class MainApplication : MvxAppCompatApplication<Setup, App>
我无法将我的片段传送到 运行,我收到以下异常。 MVVM 交叉异常:无法创建片段。使用 Android 支持片段时使用 MvxAppCompatViewPresenter。
这是 MainViewModel,它应该导航到第一个 fragment/ViewModel (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;
}
}
这是主要的 activity 布局:
<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>
有人可以让我知道我应该怎么做才能解决这个问题吗?我是新手!
我发现我的问题是我必须更改 MainApplication.cs 和 Setup.cs 类。
来自 :
public class Setup : MvxAndroidSetup
到
public class Setup : MvxAppCompatSetup<App>
来自:
public class MainApplication : MvxAndroidApplication<MvxAndroidSetup<App>, App>
到
public class MainApplication : MvxAppCompatApplication<Setup, App>