ios 在iPad上运行时TypedBinding中出现InvalidProgramException [关闭]

sigwle7e  于 2023-06-07  发布在  iOS
关注(0)|答案(1)|浏览(423)

**关闭。**此题需要debugging details。目前不接受答复。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答这个问题。
2天前关闭。
这篇文章是编辑并提交审查2天前.
Improve this question
我的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]].<…>

Searching for MAUI and 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>

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

j5fpnvbx

j5fpnvbx1#

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

public string AppVersion => AppInfo.VersionString;

可以改为static属性:

public static string AppVersion => AppInfo.VersionString;

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

相关问题