如何升级MvxFragmentAttribute

时间:2018-07-11 05:19:53

标签: xamarin.android mvvmcross

我将项目从4.3更新到MvvmCross 6.0

MvxFragmentAttribute不见了,似乎已被MvxFragmentPresentationAttribute取代

但是,当使用新属性时,出现以下错误:

System.NullReferenceException: FrameLayout to show Fragment not found
at MvvmCross.Droid.Support.V7.AppCompat.MvxAppCompatViewPresenter.ShowFragment (System.Type view, MvvmCross.Platforms.Android.Presenters.Attributes.MvxFragmentPresentationAttribute attribute, MvvmCross.ViewModels.MvxViewModelRequest request) [0x00090] in <8141cf24d04c4cc6ba0be8d85f7b3f82>:0 
at MvvmCross.Platforms.Android.Presenters.MvxAndroidViewPresenter.<RegisterAttributeTypes>b__21_2 (System.Type view, MvvmCross.Presenters.Attributes.MvxBasePresentationAttribute attribute, MvvmCross.ViewModels.MvxViewModelRequest request) [0x00000] in <17df0d0bdae848b7a8a12b58d710f763>:0 
at MvvmCross.Platforms.Android.Presenters.MvxAndroidViewPresenter.Show (MvvmCross.ViewModels.MvxViewModelRequest request) [0x00014] in <17df0d0bdae848b7a8a12b58d710f763>:0 
at MvvmCross.Platforms.Android.Views.MvxAndroidViewDispatcher+<>c__DisplayClass2_0.<ShowViewModel>b__0 () [0x00000] in <17df0d0bdae848b7a8a12b58d710f763>:0 
at MvvmCross.Base.MvxMainThreadAsyncDispatcher+<>c__DisplayClass0_0.<ExecuteOnMainThreadAsync>b__0 () [0x00000] in <17df0d0bdae848b7a8a12b58d710f763>:0 
at MvvmCross.Base.MvxMainThreadAsyncDispatcher+<>c__DisplayClass1_0+<<ExecuteOnMainThreadAsync>b__0>d.MoveNext () [0x00011] in <17df0d0bdae848b7a8a12b58d710f763>:0 
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <f32579baafc1404fa37ba3ec1abdc0bd>:0 
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__6_0 (System.Object state) [0x00000] in <f32579baafc1404fa37ba3ec1abdc0bd>:0 
at Android.App.SyncContext+<>c__DisplayClass2_0.<Post>b__0 () [0x00000] in <263adecfa58f4c449f1ff56156d886fd>:0 
at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in <263adecfa58f4c449f1ff56156d886fd>:0 
at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00008] in <263adecfa58f4c449f1ff56156d886fd>:0 
at (wrapper dynamic-method) System.Object.515beccd-4ae3-42be-a59f-9c8897c1459b(intptr,intptr)

不幸的是,这并不能告诉我哪个FrameLayout甚至哪个Fragment和堆栈跟踪都不包含我的代码。

但是在此之前只有一个碎片被击中:

旧属性用法:

[MvxFragment(typeof(MainViewModel), Resource.Id.main_frame_layout)]

新属性的用法:

[MvxFragmentPresentation(typeof(MainViewModel), Resource.Id.main_frame_layout)]

是的,main_frame_layout确实存在于我的main_view布局中,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/main_draw_layout"
    android:fitsSystemWindows="true">
    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/main_frame_layout"
            android:layout_centerInParent="true" />
    </android.support.design.widget.CoordinatorLayout>
    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:id="@+id/main_navigation_layout" />
</android.support.v4.widget.DrawerLayout>

1 个答案:

答案 0 :(得分:0)

我正在从ViewModel的Start方法调用_navigationService.Navigate来显示片段。但是,生命周期已在MvvmCross 5中更改,详细信息here。现在,Start方法在显示视图之前或至少在完成显示之前触发。这意味着该活动存在,但布局尚未膨胀,因此main_frame_layout还不存在。

解决方法是将“导航”调用移至新的ViewAppeared方法。