c# - 如何使用 MvvmCross 6.x 在 TabLayout 中正确使用 Fragments
问题描述
问题
我正在尝试使用 MvvmCross 6.1.2 使用 TabLayout 和 Fragments 制作一个非常简单的概念证明。为此,我使用 TabLayout 和 ViewPager 实现了一个活动,它应该有两个选项卡——每个选项卡都包含一个只有一个 TextView 的不同片段。
但是当应该显示此活动时,我收到一个异常,然后在运行时崩溃:
在presenter字典中没有配置类型MvxTabLayoutPresentationAttribute
代码
这就是我的代码的样子,我按照Playground 示例和文档实现了它:
AppStart.cs:
public class AppStart : MvxAppStart
{
private readonly IMvxNavigationService _mvxNavigationService;
public AppStart(IMvxApplication app, IMvxNavigationService mvxNavigationService)
: base(app, mvxNavigationService)
{
_mvxNavigationService = mvxNavigationService;
}
protected override void NavigateToFirstViewModel(object hint = null)
{
Mvx.Resolve<IMvxNavigationService>().Navigate<TabLayoutViewModel>();
}
}
TabLayoutViewModel.cs
public class TabLayoutViewModel: MvxViewModel
{
public override async Task Initialize()
{
await base.Initialize();
var tasks = new List<Task>();
tasks.Add(Mvx.Resolve<IMvxNavigationService>().Navigate<FragmentTab1ViewModel>());
tasks.Add(Mvx.Resolve<IMvxNavigationService>().Navigate<FragmentTab2ViewModel>());
await Task.WhenAll(tasks);
}
}
FragmentTab1ViewModel.cs(和 FragmentTab2ViewModel.cs 同样)
public class FragmentTab1ViewModel : MvxViewModel
{
public override Task Initialize()
{
return base.Initialize();
}
}
TabLayoutViewController.cs
[MvxActivityPresentation]
[Activity(Label = "", ScreenOrientation = ScreenOrientation.Portrait, LaunchMode = LaunchMode.SingleTask, Theme = "@style/LoginTheme")]
public class TabLayoutViewController: MvxAppCompatActivity<TabLayoutViewModel>
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.TabLayoutView);
var set = this.CreateBindingSet<TabLayoutViewController, TabLayoutViewModel>();
set.Apply();
}
}
TabLayoutView.axml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alwaysDrawnWithCache="false"
android:background="#f4f4f4"
android:minWidth="25px"
android:minHeight="25px">
<android.support.design.widget.TabLayout
android:id="@+id/tabsTeste"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
app:tabGravity="center"
app:tabMode="scrollable" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpagerTeste"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.design.widget.CoordinatorLayout>
FragmentTab1ViewController.cs(和 FragmentTab2ViewController.cs 同样)
[MvxTabLayoutPresentation(ActivityHostViewModelType = typeof(TabLayoutViewModel), ViewPagerResourceId = Resource.Id.viewpagerTest, TabLayoutResourceId = Resource.Id.tabsTest, Title = "Tab A")]
[Register("smartSolution.coleta.droid.view.FragmentTab1ViewController")]
public class FragmentTab1ViewController : MvxFragment<FragmentTab1ViewModel>
{
public override Android.Views.View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
var view = this.BindingInflate(Resource.Layout.FragmentTab1View, null);
inflater.Inflate(Resource.Layout.FragmentTab1View, container, true);
var set = this.CreateBindingSet<FragmentTab1ViewController, FragmentTab1ViewModel>();
set.Apply();
return view;
}
}
(FragmentTab1View.axml 和 FragmentTab2View.axml 只是带有 TextView 的 LinearLayouts)
问题
- 抛出异常的原因是什么?
- 这是使用片段实现 TabLayout 的推荐方法吗?
- 遵循 MvvmCross 6.x 的良好实践,可以做些什么来解决这个问题?
解决方案
抛出该异常是因为该属性未AttributeTypesToActionsDictionary
在Presenter
.
在代码中,您可以看到在RegisterAttributeTypes方法中它已注册,但考虑到它位于MvxAppCompatViewPresenter
. 此外,在文档中它指出该属性仅适用于AppCompat
.
鉴于您遇到了该异常,我可以假设正在使用非 AppCompat 演示者,因此您正在使用MvxAndroidSetup
.
要解决此问题,请确保您使用的是AppCompat
类,特别是MvxAppCompatSetup
如果您有一个自定义设置,则继承自MvxAppCompatViewPresenter
。MvxAppCompatApplication
如果强制您使用. AppCompat
_Setup
关于异常评论的更新MvvmCross.Exceptions.MvxException: ViewPager not found
我认为问题在于您正在导航到子视图模型,Initialize
而不是在创建选项卡视图之后执行此操作,因此当您尝试导航到子视图时 ViewPager 可能尚未初始化,因此找不到。
因此,在Playground Viewmodel中,您应该有一个命令,该命令调用一个方法来在您的 ViewModel 上进行导航:
...
ShowInitialViewModelsCommand = new MvxAsyncCommand(ShowInitialViewModels);
...
public IMvxAsyncCommand ShowInitialViewModelsCommand { get; private set; }
...
private async Task ShowInitialViewModels()
{
var tasks = new List<Task>();
tasks.Add(Mvx.Resolve<IMvxNavigationService>().Navigate<FragmentTab1ViewModel>());
tasks.Add(Mvx.Resolve<IMvxNavigationService>().Navigate<FragmentTab2ViewModel>());
await Task.WhenAll(tasks);
}
和在Playground View中一样,您应该在以下命令中调用命令TabLayoutViewController
:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.TabLayoutView);
var set = this.CreateBindingSet<TabLayoutViewController, TabLayoutViewModel>();
set.Apply();
if (bundle == null)
{
ViewModel.ShowInitialViewModelsCommand.Execute();
}
}
HIH
推荐阅读
- vue.js - 如何清除所有路由并推送到索引
- python - 神经机器翻译:IndexError:维度指定为 0 但张量没有维度
- flutter - 如何像 camscanner 一样在颤振中获得神奇的色彩增强
- spring - Spring RESTful Web 服务身份验证
- wildfly - 如何在 JBeret 中强制 EJB 调度程序
- sql - 为什么这个带有 with 语句的 postgresql 批量更新语句不起作用?
- macos-catalina - 我的 Mac 系统偏好设置中没有内置麦克风选项
- ios - Flutter iOS 应用体积巨大,如何减小?
- c++ - 初始化包含固定大小数组的 C++ 结构
- python - 在单独的数据框中用匹配值替换列名