在 iPad 上运行时 TypedBinding 中的 InvalidProgramException

InvalidProgramException in TypedBinding when running on iPad

提问人:Glorfindel 提问时间:6/2/2023 最后编辑:Glorfindel 更新时间:8/23/2023 访问量:61

问:

我的 MAUI 应用程序在调试模式下的 iOS 模拟器上运行良好,但是当我在 iPad 上以发布模式运行它时,打开某个屏幕时出现以下神秘错误:

System.Reflection.TargetInvocationException: Arg_TargetInvocationException
 ---> System.InvalidProgramException
       at Microsoft.Maui.Controls.Internals.TypedBinding`2[[OIL.ISA.ViewModels.SettingsViewModel, OIL.ISA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[System.String, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].ApplyCore(Object sourceObject, BindableObject target, BindableProperty property, Boolean fromTarget)
       at Microsoft.Maui.Controls.Internals.TypedBinding`2[[OIL.ISA.ViewModels.SettingsViewModel, OIL.ISA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[System.String, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].Apply(Object context, BindableObject bindObj, BindableProperty targetProperty, Boolean fromBindingContextChanged)
   at Microsoft.Maui.Controls.BindableObject.ApplyBindings(Boolean skipBindingContext, Boolean fromBindingContextChanged)
       at Microsoft.Maui.Controls.BindableObject.SetInheritedBindingContext(BindableObject bindable, Object value)
   at Microsoft.Maui.Controls.Element.SetChildInheritedBindingContext(Element child, Object context)
       at Microsoft.Maui.Controls.Element.<OnBindingContextChanged>b__82_0(BindableObject child, Object bc)
   at Microsoft.Maui.Controls.BindableObjectExtensions.PropagateBindingContext[Element](BindableObject self, IEnumerable`1 children, Action`2 setChildBindingContext)
   at Microsoft.Maui.Controls.Element.OnBindingContextChanged()
       at Microsoft.Maui.Controls.VisualElement.OnBindingContextChanged()
   at Microsoft.Maui.Controls.View.OnBindingContextChanged()
   at Microsoft.Maui.Controls.BindableObject.SetInheritedBindingContext(BindableObject bindable, Object value)
       at Microsoft.Maui.Controls.Element.SetChildInheritedBindingContext(Element child, Object context)
   at Microsoft.Maui.Controls.Element.<OnBindingContextChanged>b__82_0(BindableObject child, Object bc)
       at Microsoft.Maui.Controls.BindableObjectExtensions.PropagateBindingContext[Element](BindableObject self, IEnumerable`1 children, Action`2 setChildBindingContext)
   at Microsoft.Maui.Controls.Element.OnBindingContextChanged()
   at Microsoft.Maui.Controls.VisualElement.OnBindingContextChanged()
       at Microsoft.Maui.Controls.View.OnBindingContextChanged()
   at Microsoft.Maui.Controls.BindableObject.SetInheritedBindingContext(BindableObject bindable, Object value)
   at Microsoft.Maui.Controls.Element.SetChildInheritedBindingContext(Element child, Object context)
       at Microsoft.Maui.Controls.TemplatedPage.SetChildInheritedBindingContext(Element child, Object context)
   at Microsoft.Maui.Controls.Element.<OnBindingContextChanged>b__82_0(BindableObject child, Object bc)
       at Microsoft.Maui.Controls.BindableObjectExtensions.PropagateBindingContext[Element](BindableObject self, IEnumerable`1 children, Action`2 setChildBindingContext)
   at Microsoft.Maui.Controls.Element.OnBindingContextChanged()
   at Microsoft.Maui.Controls.VisualElement.OnBindingContextChanged()
       at Microsoft.Maui.Controls.Page.OnBindingContextChanged()
   at Microsoft.Maui.Controls.ContentPage.OnBindingContextChanged()
   at Microsoft.Maui.Controls.BindableObject.BindingContextPropertyChanged(BindableObject bindable, Object oldvalue, Object newvalue)
       at Microsoft.Maui.Controls.BindableObject.SetValueActual(BindableProperty property, BindablePropertyContext context, Object value, Boolean currentlyApplying, SetValueFlags attributes, Boolean silent)
       at Microsoft.Maui.Controls.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes)
   at Microsoft.Maui.Controls.BindableObject.SetValue(BindableProperty property, Object value, Boolean fromStyle, Boolean checkAccess)
       at Microsoft.Maui.Controls.BindableObject.SetValue(BindableProperty property, Object value)
   at Microsoft.Maui.Controls.BindableObject.set_BindingContext(Object value)
   at OIL.ISA.Views.SettingsPage..ctor()
       at System.Reflection.RuntimeConstructorInfo.InternalInvoke(Object , Object[] , Boolean )
   Exception_EndOfInnerExceptionStack
   at ObjCRuntime.Runtime.ThrowException(IntPtr )
   at UIKit.UIApplication.UIApplicationMain(Int32 , String[] , IntPtr , IntPtr )
       at UIKit.UIApplication.Main(String[] , Type , Type )
   at OIL.ISA.Platforms.iOS.Program.Main(String[] args)
    Unhandled managed exception: Arg_TargetInvocationException (System.Reflection.TargetInvocationException)
   at ObjCRuntime.Runtime.ThrowException(IntPtr )
   at UIKit.UIApplication.UIApplicationMain(Int32 , String[] , IntPtr , IntPtr )
   at UIKit.UIApplication.Main(String[] , Type , Type )
   at OIL.ISA.Platforms.iOS.Program.Main(String[] args)
 (System.InvalidProgramException)
   at Microsoft.Maui.Controls.Internals.TypedBinding`2[[OIL.ISA.ViewModels.SettingsViewModel, OIL.ISA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[System.String, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].ApplyCore(Object sourceObject, BindableObject target, BindableProperty property, Boolean fromTarget)
   at Microsoft.Maui.Controls.Internals.TypedBinding`2[[OIL.ISA.ViewModels.SettingsViewModel, OIL.ISA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[System.String, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].<…>

搜索 MAUI 和 InvalidProgramException 不会给出有意义的结果。此异常的原因是什么?


根据要求,以下是一些调试详细信息。视图模型如下所示:

public class SettingsViewModel : MainMenuPageViewModel {
    public SettingsViewModel(ViewModelServices services, IPushNotificationsService pushNotificationsService, IDatabaseUploader databaseUploader, ISecureStorage secureStorage) : base(services) {
        _pushNotificationsService = pushNotificationsService;
        _databaseUploader = databaseUploader;
        _databaseUploader.OnProgressChanged += OnDatabaseUploadProgressChanged;
        _secureStorage = secureStorage;

        Title = BaseApp.Settings;
        PushNotificationsEnabled = pushNotificationsService.DeviceToken != null;
        PushNotificationsEnabledMessage = PushNotificationsEnabled ? BaseApp.PushNotificationsEnabled : BaseApp.PushNotificationsNotEnabled;
        Logout = new Command(DoLogout);
        SendDatabase = new Command(UploadDatabase);
        ShowPushNotificationDetails = new Command(ShowNotificationDetails);

        using var context = new Context();
        UserInformation = BaseApp.LoggedInAs + " " + LocalSettings.GetUsername(context);
        UpdateSettingsInformation();
        Settings.OnRefresh += (sender, e) => UpdateSettingsInformation();
    }

    private void UpdateSettingsInformation() {
        StoreInformation = Settings.Store == null ? "" : Settings.Store.Number + " - " + Settings.Store.Name;
        OnPropertyChanged(nameof(StoreInformation));
    }

    private void OnDatabaseUploadProgressChanged(object sender, ProgressChangedEventArgs e) {
        Progress = e.ProgressPercentage / 100f;
    }

    private readonly IPushNotificationsService _pushNotificationsService;
    private readonly IDatabaseUploader _databaseUploader;
    private readonly ISecureStorage _secureStorage;

    public string UserInformation { get; }
    public string StoreInformation { get; private set; }
    public bool PushNotificationsEnabled { get; }
    public string PushNotificationsEnabledMessage { get; }
    public string AppVersion => AppInfo.VersionString;

    public ICommand Logout { get; }
    public ICommand SendDatabase { get; }
    public ICommand ShowPushNotificationDetails { get; }

    ...
}

和页面本身

<?xml version="1.0" encoding="UTF-8" ?>
<ContentPage
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:controls="clr-namespace:OIL.ISA.Controls"
    xmlns:vm="clr-namespace:OIL.ISA.ViewModels"
    xmlns:r="clr-namespace:OIL.ISA.Resources"
    x:DataType="vm:SettingsViewModel"
    x:Class="OIL.ISA.Views.SettingsPage"
    Title="{Binding Title}"
    Shell.NavBarIsVisible="{Binding NavBarIsVisible}">
    <ContentPage.Content>
        <AbsoluteLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
            <Image AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0,0,1,1"
                   Source="background.jpg" Aspect="AspectFill" />
            <StackLayout AbsoluteLayout.LayoutFlags="PositionProportional" AbsoluteLayout.LayoutBounds=".5,0" WidthRequest="320"
                         BackgroundColor="#D0FFFFFF" Spacing="16" Padding="16" Margin="0,16">
                <Label Text="{Binding UserInformation}" HorizontalOptions="Center" />
                <Label Text="{Binding StoreInformation}" HorizontalOptions="Center" />
                <Button Style="{StaticResource SettingsButton}"
                        Text="{x:Static r:BaseApp.LogoutButton}"
                        Command="{Binding Logout}" />
                <Button Style="{StaticResource SettingsButton}"
                        Text="{x:Static r:BaseApp.SendDatabaseButton}"
                        Command="{Binding SendDatabase}" />
                <StackLayout Orientation="Horizontal">
                    <StackLayout.GestureRecognizers>    
                        <TapGestureRecognizer Command="{Binding ShowPushNotificationDetails}" />
                    </StackLayout.GestureRecognizers>
                    <CheckBox IsChecked="{Binding PushNotificationsEnabled}" VerticalOptions="Center" IsEnabled="False" />
                    <Label Text="{Binding PushNotificationsEnabledMessage}" VerticalOptions="Center" />
                </StackLayout>
                <Label Text="{Binding Path=AppVersion, StringFormat={x:Static r:BaseApp.Version}}"
                       HorizontalOptions="Center" />
            </StackLayout>
            <controls:BlockingActivityIndicator
                AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0,0,1,1"
                IsVisible="{Binding IsBusy}"
                Message="{Binding BusyMessage}"
                HasProgress="{Binding HasProgress}"
                Progress="{Binding Progress}" />
        </AbsoluteLayout>
    </ContentPage.Content>
</ContentPage>

(但正如你所看到的,我已经找到了答案,我只需要在某个地方记录它,以防其他人遇到它。信不信由你,两个月后,在应用程序的另一个视图模型中,这种情况发生在我自己身上。这已经是谷歌的第三次点击了,我希望它能以这种方式有用)

iOS 异常 绑定 MAUI

评论


答:

0赞 Glorfindel 6/2/2023 #1

原来,我的同事使用 Resharper 来分析代码,它检测到 ViewModel 属性之一:

public string AppVersion => AppInfo.VersionString;

可以更改为属性:static

public static string AppVersion => AppInfo.VersionString;

这在 Android 或 iOS 模拟器中没有导致任何问题,但直到很久以后才在 iPad 上测试发行版时才被发现。再次将其设置为实例属性,修复了该错误。