在 MvvmCross 3.5.1 中实现片段的正确方法是什么
What's the proper way to implement Fragments in MvvmCross 3.5.1
从 3.5 升级到 3.5.1 后,我的 Xamarin.Android 项目中的 Fragments 膨胀时出现错误。
Unable to start activity ComponentInfo{xxx.xxx/md51537e9497dba76724980c8d1dff85301.MyView}: android.view.InflateException: Binary XML file line #1: Error inflating class fragment
Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class md51537e9497dba76724980c8d1dff85301.LoginView that is not a Fragment
导致此错误的原因是什么? 3.5.1 中有重大变化吗?
我的片段定义为
public class LoginFormView : MvxFragment
{
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var ignore = base.OnCreateView(inflater, container, savedInstanceState);
return this.BindingInflate(Resource.Layout.LoginFormView, null);
}
}
我已经通过 Xamarin 组件商店包含了 V4 支持。
我通过以下方式给我的片段充气:
public class LoginView : BaseMvxFragmentActivity
{
...
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.LoginView);
_loginForm = (LoginFormView)SupportFragmentManager.FindFragmentById(Resource.Id.sub_login_form);
_loginForm.ViewModel = MyViewModel.LoginFormViewModel;
}
...
}
更新 1
我的axml布局如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<!-- Snip -->
<LinearLayout
android:id="@+id/InnerRelativeLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:animateLayoutChanges="true"
android:padding="10dp">
<fragment
class="com.namespace.LoginFormView"
android:id="@+id/sub_login_form"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
local:MvxBind="Visibility ShowLoginFormViewModel,Converter=Visibility" />
更新 2
当我构建到物理设备时它工作正常
更新 3
如果我按照 CheeseBaron 的建议添加 [Register]
,则原因变为:
[MonoDroid] Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.namespace.Droid.Views.LoginFormView that is not a Fragment
更多信息:
[MonoDroid] UNHANDLED EXCEPTION:
[MonoDroid] Android.Views.InflateException: Exception of type 'Android.Views.InflateException' was thrown.
[MonoDroid] at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <IL 0x00011, 0x0004b>
[MonoDroid] at Android.Runtime.JNIEnv.CallObjectMethod (intptr,intptr,Android.Runtime.JValue*) [0x00064] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d23da369/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:195
[MonoDroid] at Android.Views.LayoutInflater.Inflate (int,Android.Views.ViewGroup,bool) [0x00082] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d23da369/source/monodroid/src/Mono.Android/platforms/android-16/src/generated/Android.Views.LayoutInflater.cs:687
[MonoDroid] at Cirrious.MvvmCross.Binding.Droid.BindingContext.MvxAndroidBindingContext.CommonInflate (int,Android.Views.ViewGroup,Cirrious.MvvmCross.Binding.Droid.Binders.IMvxLayoutInfactorFactory,bool) <IL 0x00030, 0x00136>
[MonoDroid] at Cirrious.MvvmCross.Binding.Droid.BindingContext.MvxAndroidBindingContext.BindingInflate (int,Android.Views.ViewGroup,bool) <IL 0x00015, 0x000a5>
[MonoDroid] at Cirrious.MvvmCross.Binding.Droid.BindingContext.MvxAndroidBindingContext.BindingInflate (int,Android.Views.ViewGroup) <IL 0x00004, 0x00044>
[MonoDroid] at Cirrious.MvvmCross.Binding.Droid.BindingContext.MvxBindingContextOwnerExtensions.BindingInflate (Cirrious.MvvmCross.Binding.BindingContext.IMvxBindingContextOwner,int,Android.Views.ViewGroup) <IL 0x0000f, 0x000f5>
[MonoDroid] at Cirrious.MvvmCross.Droid.Fragging.MvxFragmentActivity.SetContentView (int) <IL 0x00003, 0x00037>
[MonoDroid] at com.namespace.Droid.Views.LoginView.OnCreate (Android.OS.Bundle) [0x0000e] in /xxx/Ccom.namespace.Droid/Views/LoginView.cs:34
[MonoDroid] at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d23da369/source/monodroid/src/Mono.Android/platforms/android-16/src/generated/Android.App.Activity.cs:2374
[MonoDroid] at (wrapper dynamic-method) object.facfe989-fd2b-40d3-bf63-959b844af987 (intptr,intptr,intptr) <IL 0x00017, 0x0002b>
Xamarin.Android 5.1 引入了一些重大更改。这意味着您需要在所有片段中添加 [Register("app.namespace.FragmentName")]
。
有关详细信息,请参阅:
https://github.com/MvvmCross/MvvmCross/issues/987
http://forums.xamarin.com/discussion/37277/stable-release-xamarin-android-5-1-0-breaking-changes-new-features-and-bug-fixes
编辑:
好的!这与#990 无关。问题是由于某些原因 MvxFragment
无法膨胀,因为它不被识别为片段。当更改为最小目标 15 和 FullFragging MvvmCross nuget 时,它开始工作。
还要记住,所有 Java 命名空间需要小写。所以当你使用 [Register]
时,你必须遵守它。
您的示例也有两个 MainLauncher
活动,所以我不得不删除默认的 MainActivity
。你的FirstViewFragment
也定义了一个Activity
,为什么要那样做???
简而言之,我做了以下更改:
- 最低目标 15
- 用 FullFragging 替换 Fragging
- 从
FirstViewFragment
中删除 [Activity]
个标志
[Register]
FirstViewFragment
中的小写命名空间
- 将
FirstView
从 MvxFragmentActivity
更改为 MvxActivity
- 删除
MainActivity
- 将
SupportFragmentManager
替换为FragmentManager
class
FirstView.axml
中的小写命名空间
然后就可以了。为什么MvvmCross中framents的支持版本不起作用需要调查
从 3.5 升级到 3.5.1 后,我的 Xamarin.Android 项目中的 Fragments 膨胀时出现错误。
Unable to start activity ComponentInfo{xxx.xxx/md51537e9497dba76724980c8d1dff85301.MyView}: android.view.InflateException: Binary XML file line #1: Error inflating class fragment
Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class md51537e9497dba76724980c8d1dff85301.LoginView that is not a Fragment
导致此错误的原因是什么? 3.5.1 中有重大变化吗?
我的片段定义为
public class LoginFormView : MvxFragment
{
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var ignore = base.OnCreateView(inflater, container, savedInstanceState);
return this.BindingInflate(Resource.Layout.LoginFormView, null);
}
}
我已经通过 Xamarin 组件商店包含了 V4 支持。
我通过以下方式给我的片段充气:
public class LoginView : BaseMvxFragmentActivity
{
...
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.LoginView);
_loginForm = (LoginFormView)SupportFragmentManager.FindFragmentById(Resource.Id.sub_login_form);
_loginForm.ViewModel = MyViewModel.LoginFormViewModel;
}
...
}
更新 1
我的axml布局如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<!-- Snip -->
<LinearLayout
android:id="@+id/InnerRelativeLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:animateLayoutChanges="true"
android:padding="10dp">
<fragment
class="com.namespace.LoginFormView"
android:id="@+id/sub_login_form"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
local:MvxBind="Visibility ShowLoginFormViewModel,Converter=Visibility" />
更新 2
当我构建到物理设备时它工作正常
更新 3
如果我按照 CheeseBaron 的建议添加 [Register]
,则原因变为:
[MonoDroid] Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.namespace.Droid.Views.LoginFormView that is not a Fragment
更多信息:
[MonoDroid] UNHANDLED EXCEPTION:
[MonoDroid] Android.Views.InflateException: Exception of type 'Android.Views.InflateException' was thrown.
[MonoDroid] at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <IL 0x00011, 0x0004b>
[MonoDroid] at Android.Runtime.JNIEnv.CallObjectMethod (intptr,intptr,Android.Runtime.JValue*) [0x00064] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d23da369/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:195
[MonoDroid] at Android.Views.LayoutInflater.Inflate (int,Android.Views.ViewGroup,bool) [0x00082] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d23da369/source/monodroid/src/Mono.Android/platforms/android-16/src/generated/Android.Views.LayoutInflater.cs:687
[MonoDroid] at Cirrious.MvvmCross.Binding.Droid.BindingContext.MvxAndroidBindingContext.CommonInflate (int,Android.Views.ViewGroup,Cirrious.MvvmCross.Binding.Droid.Binders.IMvxLayoutInfactorFactory,bool) <IL 0x00030, 0x00136>
[MonoDroid] at Cirrious.MvvmCross.Binding.Droid.BindingContext.MvxAndroidBindingContext.BindingInflate (int,Android.Views.ViewGroup,bool) <IL 0x00015, 0x000a5>
[MonoDroid] at Cirrious.MvvmCross.Binding.Droid.BindingContext.MvxAndroidBindingContext.BindingInflate (int,Android.Views.ViewGroup) <IL 0x00004, 0x00044>
[MonoDroid] at Cirrious.MvvmCross.Binding.Droid.BindingContext.MvxBindingContextOwnerExtensions.BindingInflate (Cirrious.MvvmCross.Binding.BindingContext.IMvxBindingContextOwner,int,Android.Views.ViewGroup) <IL 0x0000f, 0x000f5>
[MonoDroid] at Cirrious.MvvmCross.Droid.Fragging.MvxFragmentActivity.SetContentView (int) <IL 0x00003, 0x00037>
[MonoDroid] at com.namespace.Droid.Views.LoginView.OnCreate (Android.OS.Bundle) [0x0000e] in /xxx/Ccom.namespace.Droid/Views/LoginView.cs:34
[MonoDroid] at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/monodroid-mavericks-monodroid-5.1-series/d23da369/source/monodroid/src/Mono.Android/platforms/android-16/src/generated/Android.App.Activity.cs:2374
[MonoDroid] at (wrapper dynamic-method) object.facfe989-fd2b-40d3-bf63-959b844af987 (intptr,intptr,intptr) <IL 0x00017, 0x0002b>
Xamarin.Android 5.1 引入了一些重大更改。这意味着您需要在所有片段中添加 [Register("app.namespace.FragmentName")]
。
有关详细信息,请参阅: https://github.com/MvvmCross/MvvmCross/issues/987 http://forums.xamarin.com/discussion/37277/stable-release-xamarin-android-5-1-0-breaking-changes-new-features-and-bug-fixes
编辑:
好的!这与#990 无关。问题是由于某些原因 MvxFragment
无法膨胀,因为它不被识别为片段。当更改为最小目标 15 和 FullFragging MvvmCross nuget 时,它开始工作。
还要记住,所有 Java 命名空间需要小写。所以当你使用 [Register]
时,你必须遵守它。
您的示例也有两个 MainLauncher
活动,所以我不得不删除默认的 MainActivity
。你的FirstViewFragment
也定义了一个Activity
,为什么要那样做???
简而言之,我做了以下更改:
- 最低目标 15
- 用 FullFragging 替换 Fragging
- 从
FirstViewFragment
中删除 [Register]
FirstViewFragment
中的小写命名空间
- 将
FirstView
从MvxFragmentActivity
更改为MvxActivity
- 删除
MainActivity
- 将
SupportFragmentManager
替换为FragmentManager
class
FirstView.axml
中的小写命名空间
[Activity]
个标志
然后就可以了。为什么MvvmCross中framents的支持版本不起作用需要调查