MVVMCross Android 应用程序崩溃 - System.Reflection.AmbiguousMatchException:找到不明确的匹配项

MVVMCross Android App Crash- System.Reflection.AmbiguousMatchException: Ambiguous match found

从 MvvmCross 5 切换到 7 后,我一转到此页面就在 set.Apply(); 收到 AmbiguousMatchException,但我无法弄清楚是什么原因或如何解决它。同样的代码以前可以用,但现在不行了。

我试图删除 set.Bind 函数并删除了崩溃异常,但随后地址字段为空(与仅添加 try catch 的效果相同)。我也尝试只绑定4个中的一个,但仍然出现异常。

以下是所有异常详情:

[AppCenterCrashes] Unhandled Exception from source=AndroidEnvironment
[AppCenterCrashes] System.Reflection.AmbiguousMatchException: Ambiguous match found.
[AppCenterCrashes]   at System.RuntimeType.GetPropertyImpl (System.String name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Type returnType, System.Type[] types, System.Reflection.ParameterModifier[] modifiers) [0x00057] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/referencesource/mscorlib/system/rttype.cs:3344 
[AppCenterCrashes]   at System.Type.GetProperty (System.String name, System.Reflection.BindingFlags bindingAttr) [0x0000e] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/Common/src/CoreLib/System/Type.cs:231 
[AppCenterCrashes]   at System.Type.GetProperty (System.String name) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/Common/src/CoreLib/System/Type.cs:226 
[AppCenterCrashes]   at MvvmCross.Binding.Bindings.Target.Construction.MvxTargetBindingFactoryRegistry.TryCreateReflectionBasedBinding (System.Object target, System.String targetName, MvvmCross.Binding.Bindings.Target.IMvxTargetBinding& binding) [0x00024] in /_/MvvmCross/Binding/Bindings/Target/Construction/MvxTargetBindingFactoryRegistry.cs:45 
[AppCenterCrashes]   at MvvmCross.Binding.Bindings.Target.Construction.MvxTargetBindingFactoryRegistry.CreateBinding (System.Object target, System.String targetName) [0x0000e] in /_/MvvmCross/Binding/Bindings/Target/Construction/MvxTargetBindingFactoryRegistry.cs:22 
[AppCenterCrashes]   at MvvmCross.Binding.Bindings.MvxFullBinding.CreateTargetBinding (System.Object target) [0x00000] in /_/MvvmCross/Binding/Bindings/MvxFullBinding.cs:146 
[AppCenterCrashes]   at MvvmCross.Binding.Bindings.MvxFullBinding..ctor (MvvmCross.Binding.MvxBindingRequest bindingRequest) [0x00028] in /_/MvvmCross/Binding/Bindings/MvxFullBinding.cs:59 
[AppCenterCrashes]   at MvvmCross.Binding.Binders.MvxFromTextBinder.BindSingle (MvvmCross.Binding.MvxBindingRequest bindingRequest) [0x00000] in /_/MvvmCross/Binding/Binders/MvxFromTextBinder.cs:52 
[AppCenterCrashes]   at MvvmCross.Binding.Binders.MvxFromTextBinder+<>c__DisplayClass2_0.<Bind>b__0 (MvvmCross.Binding.Bindings.MvxBindingDescription description) [0x00000] in /_/MvvmCross/Binding/Binders/MvxFromTextBinder.cs:34 
[AppCenterCrashes]   at System.Linq.Enumerable+SelectArrayIterator`2[TSource,TResult].MoveNext () [0x0003a] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/System.Linq/src/System/Linq/Select.cs:224 
[AppCenterCrashes]   at MvvmCross.Binding.BindingContext.MvxBindingContextOwnerExtensions.AddBinding (MvvmCross.Binding.BindingContext.IMvxBindingContextOwner view, System.Object target, MvvmCross.Binding.Bindings.MvxBindingDescription bindingDescription, System.Object clearKey) [0x0000b] in /_/MvvmCrxt.MvxBindingContextOwnerExtensions.AddBindings (MvvmCross.Binding.BindingContext.IMvxBindingContextOwner view, System.Object target, System.Collections.Generic.IEnumerable`1[T] bindings, System.Object clearKey) [0x0001d] in /_/MvvmCross/Binding/BindingContext/MvxBindingContextOwnerExtensions.cs:61 
[AppCenterCrashes]   at MvvmCross.Binding.BindingContext.MvxBindingContextOwnerExtensions.AddBindings (MvvmCross.Binding.BindingContext.IMvxBindingContextOwner view, System.Object target, System.Collections.Generic.IEnumerable`1[T] bindingDescriptions, System.Object clearKey) [0x00018] in /_/MvvmCross/Binding/BindingContext/MvxBindingContextOwnerExtensions.cs:82 
[AppCenterCrashes]   at MvvmCross.Binding.BindingContext.MvxBindingContextOwnerExtensions.AddBinding (MvvmCross.Binding.BindingContext.IMvxBindingContextOwner view, System.Object target, MvvmCross.Binding.Bindings.MvxBindingDescription bindingDescription, System.Object clearKey) [0x0000b] in /_/MvvmCross/Binding/BindingContext/MvxBindingContextOwnerExtensions.cs:75 
[AppCenterCrashes]   at MvvmCross.Binding.BindingContext.MvxBaseFluentBindingDescription`1[TTarget].Apply () [0x00007] in /_/MvvmCross/Binding/BindingContext/MvxBaseFluentBindingDescription.cs:277 
[AppCenterCrashes]   at MvvmCross.Binding.BindingContext.MvxFluentBindingDescriptionSet`2[TOwningTarget,TSource].Apply () [0x00016] in /_/MvvmCross/Binding/BindingContext/MvxFluentBindingDescriptionSet.cs:65 
[AppCenterCrashes]   at Mobile.Droid.Fragments.MoveProfile.MoveProfileMoveDetailsFragment.SetBinding () [0x00098] in /Users/macbookpro/Documents/GitHub/Mobile/Mobile.Droid/Fragments/Mov
[Mono] Requesting loading reference 5 (of 6) of /storage/emulated/0/Android/data/com.saamer/files/.__override__/Microsoft.AppCenter.Crashes.dll
[Mono] Loading reference 5 of /storage/emulated/0/Android/data/com.saamer/files/.__override__/Microsoft.AppCenter.Crashes.dll asmctx DEFAULT, looking for System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
[Mono] Assembly Ref addref Microsoft.AppCenter.Crashes[0xf0db4940] -> System.Core[0xf0d90d40]: 16
[AppCenterCrashes] Saving uncaught exception.
[AppCenterCrashes] Saved JSON content for ingestion into /data/user/0/com.saamer/files/error/82c8d389-5698-4779-bb4e-aea8a9689bcf.json
[AppCenterCrashes] Saved empty Throwable file in /data/user/0/com.saamer/files/error/82c8d389-5698-4779-bb4e-aea8a9689bcf.throwable
[AppCenterCrashes] Saved raw wrapper exception data into /data/user/0/com.saamer/files/error/82c8d389-5698-4779-bb4e-aea8a9689bcf.dat
[Mono] DllImport searching in: '__Internal' ('(null)').
[Mono] Searching for 'java_interop_jnienv_throw'.
[Mono] Probing 'java_interop_jnienv_throw'.

这是无效的片段代码

using Android.OS;
using Android.Runtime;
using Android.Views;


using Mobile.ViewModels.ViewModels.MoveProfile;
using MvvmCross.Binding.BindingContext;
using MvvmCross.Platforms.Android.Binding.BindingContext;
using MvvmCross.Platforms.Android.Views.Fragments;

namespace Mobile.Droid.Fragments.MoveProfile
{
    [Register("mobile.droid.fragments.moveprofile.MoveProfileMoveDetailsFragment")]
    public class MoveProfileMoveDetailsFragment : MvxFragment
    {
        private AddressInfoFragment _addressFragment;
        private AddressInfoFragment _renterAddressFragment;
        private AddressInfoFragment _mailingAddressFragment;
        private AddressInfoFragment _destinationAddressFragment;
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container,
                                          Bundle savedInstanceState)
        {
            var ignored = base.OnCreateView(inflater, container, savedInstanceState);
            var view = this.BindingInflate(Resource.Layout.fragment_move_profile_move_details, null);
            AddFragments();
            SetBinding();
            return view;
        }

        private void AddFragments()
        {
            _addressFragment = new AddressInfoFragment();
            _renterAddressFragment = new AddressInfoFragment();
            _mailingAddressFragment = new AddressInfoFragment();
            _destinationAddressFragment = new AddressInfoFragment();

            var ft = Activity.SupportFragmentManager.BeginTransaction();

            ft.Replace(Resource.Id.address_fragment, _addressFragment);
            ft.Replace(Resource.Id.renter_address_fragment, _renterAddressFragment);
            ft.Replace(Resource.Id.mailing_address_fragment, _mailingAddressFragment);
            ft.Replace(Resource.Id.destination_address_fragment, _destinationAddressFragment);

            ft.Commit();
        }

        private void SetBinding()
        {
            var owner = this as IMvxBindingContextOwner;
            var set = owner.CreateBindingSet<IMvxBindingContextOwner, MoveProfileMoveDetailsViewModel>();

            set.Bind(_addressFragment).For(fr => fr.ViewModel).To(vm => vm.AddressInfoAddressViewModel);
            set.Bind(_renterAddressFragment).For(fr => fr.ViewModel).To(vm => vm.RenterAddressInfo);
            set.Bind(_mailingAddressFragment).For(fr => fr.ViewModel).To(vm => vm.MailingAddressInfo);
            set.Bind(_destinationAddressFragment).For(fr => fr.ViewModel).To(vm => vm.DestinationAddressInfo);
            set.Apply(); //Crash here
        }
    }
}

更令人惊讶的是,编辑页面看起来几乎完全一样,而且工作得非常好。这是布局:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="wrap_content">
  <LinearLayout
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:paddingTop="36dp"
      style="@style/MainContentStyle">
    <TextView
        style="@style/TextViewStyle.Small"
        local:MvxLang="Text MOVEPROFILE_ORIGIN" />
    <TextView
        style="@style/TextViewStyle.Medium"
        local:MvxBind="Text OriginLocation" />
    <TextView
        style="@style/TextViewStyle.Small"
        local:MvxLang="Text MOVEPROFILE_DESTINATION" />
    <TextView
        style="@style/TextViewStyle.Medium"
        local:MvxBind="Text DestinationLocation" />
    <TextView
        style="@style/TextViewStyle.Header"
        local:MvxLang="Text MOVEPROFILE_PRINCIPALRESIDENCEGROUP" />
    <TextView
        style="@style/TextViewStyle.Small"
        local:MvxLang="Text MOVEPROFILE_HOMEOWNERORRENTER" />
    <TextView
        style="@style/TextViewStyle.Medium"
        local:MvxBind="Text PrincipleResidenceTypes.Selected.Title" />
    <FrameLayout
        android:id="@+id/address_fragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <include
        layout="@layout/fragment_move_profile_move_details_homeowner_details"/>
    <include
        layout="@layout/fragment_move_profile_move_details_renter_details"/>
    <TextView
        style="@style/TextViewStyle.Header"
        local:MvxLang="Text MOVEPROFILE_MAILINGADDRESSGROUP" />
    <TextView
        style="@style/TextViewStyle.Small"
        local:MvxLang="Text MOVEPROFILE_USEPRINCIPALADDR" />
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
      <TextView
          style="@style/TextViewStyle.Medium"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_toLeftOf="@+id/mailing_address_tooltip"
          local:MvxBind="Text BooleanToString(MailingAddressSameAsPrincipal)"/>
    </RelativeLayout>
    <LinearLayout
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          local:MvxBind="Visible MailingAddressNotSameAsPrincipal">
      <FrameLayout
          android:id="@+id/mailing_address_fragment"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" />
    </LinearLayout>
    <include
        layout="@layout/fragment_move_profile_move_details_destination_details"/>
  </LinearLayout>
</ScrollView>

我不知道从哪里开始调试这个问题,所以任何帮助将不胜感激。我尝试了几件事,但到目前为止都是徒劳的。

好吧,多亏了 Darius,我才能够找到解决方法!我将 Fragment 简化为它的基础,而不是 FrameLayout,使用 MvxFrameControl 并且它起作用了。这就是我的片段的样子:

using Android.OS;
using Android.Runtime;
using Android.Views;


using Mobile.ViewModels.ViewModels.MoveProfile;
using MvvmCross.Platforms.Android.Binding.BindingContext;
using MvvmCross.Platforms.Android.Views.Fragments;

namespace Mobile.Droid.Fragments.MoveProfile
{
    [Register("mobile.droid.fragments.moveprofile.MoveProfileMoveDetailsFragment")]
    public class MoveProfileMoveDetailsFragment : MvxFragment<MoveProfileMoveDetailsViewModel>
    {
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container,
                                          Bundle savedInstanceState)
        {
            var ignored = base.OnCreateView(inflater, container, savedInstanceState);
            var view = this.BindingInflate(Resource.Layout.fragment_move_profile_move_details, null);
            return view;
        }        
    }
}

在我的布局中不是这个:

<FrameLayout
     android:id="@+id/address_fragment"
     android:layout_width="match_parent"
     android:layout_height="wrap_content" />

我用过这个:

<MvxFrameControl
     local:MvxBind="DataContext AddressInfoAddressViewModel"
     local:MvxTemplate="@layout/fragment_address_info"
     android:layout_width="match_parent"
     android:layout_height="wrap_content" />