MVVM交叉异常:无法创建片段.使用Android支持片段时,请使用MvxAppCompatViewPresenter



我在运行我的片段时遇到了问题,我得到了以下异常。MVVM交叉异常:无法创建片段。使用Android支持片段时,请使用MvxAppCompatViewPresenter。

这是MainViewModel,它应该导航到第一个片段/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;
}
}

这是主要的活动布局:

<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>

相关内容

  • 没有找到相关文章

最新更新