在 iPad 上运行时 TypedBinding 中出现 InvalidProgramException

问题描述 投票:0回答:1

我的 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>

(但正如你所看到的,我已经找到了答案,我只需要在某个地方记录它,以防其他人遇到它。无论你相信与否,两个月后,在另一个视图中,这发生在我自己身上应用程序中的模型。这是现在 Google 中的第三个热门,我希望这样有用)

ios exception binding maui
1个回答
0
投票

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

public string AppVersion => AppInfo.VersionString;

可以更改为

static
属性:

public static string AppVersion => AppInfo.VersionString;

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

© www.soinside.com 2019 - 2024. All rights reserved.