Xamrin表单NavigationPage System.InvalidCastException

我无法弄清楚为什么它会返回这个错误,我开始认为Visual Studio for Mac中有一些错误可以保留内存中的内容,我已经转储了Bin和obj文件夹,这对我没有任何影响我的所有包裹都是最新的 .

堆栈跟踪...

System.InvalidCastException:指定的强制转换无效 . at at(wrapper castclass)System.Object:__ castclass_with_cache(object,intptr,intptr)位于D:\ agent_work \ 2 \ s \中的Xamarin.Forms.Internals.Registrar1 [TRegistrable] .GetHandler(System.Type type)[0x0001b] Xamarin.Forms.Core \ Registrar.cs:37在D:\ agent_work \ 2 \ s \ Xamarin.Forms中的Xamarin.Forms.Internals.Registrar1 [TRegistrable] .GetHandlerForObject [TOut](System.Object obj)[0x0002c] . Core \ Registrar.cs:81 at Xamarin.Forms.Platform.iOS.Platform.CreateRenderer(Xamarin.Forms.VisualElement element)[0x00005] in <9a5948fff8254b13a3d593c76c73b9b1>:0 at Xamarin.Forms.Platform.iOS.VisualElementPackager.OnChildAdded(Xamarin) .Forms.VisualElement view)[0x0002a]在<9a5948fff8254b13a3d593c76c73b9b1>:0中Xamarin.Forms.Platform.iOS.VisualElementPackager.Load()[0x0001e] in <9a5948fff8254b13a3d593c76c73b9b1>:0 at Xamarin.Forms.Platform.iOS.VisualElementRenderer1 [TElement在<9a5948fff8254b13a3d593c76c73b9b1>中的.SetElement(TElement元素)[0x000cc]:在Xamarin.Forms.Platform.iOS.VisualElementRenderer1中的0 [TEle在Xamarin.Forms.Platform.iOS.Platform.CreateRenderer(Xamarin.Forms.VisualElement元素)的<9a5948fff8254b13a3d593c76c73b9b1>:0中的.Xamarin.Forms.Platform.iOS.IVisualElementRenderer.SetElement(Xamarin.Forms.VisualElement element)[0x00000] )[0xa14]在<9a5948fff8254b13a3d593c76c73b9b1>:0中Xamarin.Forms.Platform.iOS.VisualElementPackager.OnChildAdded(Xamarin.Forms.VisualElement视图)[0x0002a]在<9a5948fff8254b13a3d593c76c73b9b1>:0处于Xamarin.Forms.Platform.iOS.VisualElementPackager . 负载()[0x0001e]在<9a5948fff8254b13a3d593c76c73b9b1>:0在Xamarin.Forms.Platform.iOS.VisualElementRenderer1 [TElement] .SetElement(TElement元件)[0x000cc]在<9a5948fff8254b13a3d593c76c73b9b1>:0在Xamarin.Forms.Platform.iOS.VisualElementRenderer1 [TElement] .Xamarin.Forms.Platform.iOS.IVisualElementRenderer.SetElement(Xamarin.Forms.VisualElement element)[0x00000] in <9a5948fff8254b13a3d593c76c73b9b1>:0 at Xamarin.Forms.Platform.iOS.Platform.CreateRenderer(Xamarin.Forms.VisualElement) eleme nt)[0xa14]在<9a5948fff8254b13a3d593c76c73b9b1>:0中Xamarin.Forms.Platform.iOS.VisualElementPackager.OnChildAdded(Xamarin.Forms.VisualElement view)[0x0002a] in <9a5948fff8254b13a3d593c76c73b9b1>:0 at Xamarin.Forms.Platform.iOS.VisualElementPackager .Laad()[0x0001e]位于<9a5948fff8254b13a3d593c76c73b9b1>:0中的Xamarin.Forms.Platform.iOS.PageRenderer.ViewDidLoad()[0x00086] in <9a5948fff8254b13a3d593c76c73b9b1>:0 at at(包装器托管到原生)ObjCRuntime.Messaging:位于/Users/builder/data/lanes/5520/51128b8c/source/xamarin-macios/src/build/ios/native/UIKit/UIViewController.g的UIKit.UIViewController.get_View()[0x00030]中的IntPtr_objc_msgSendSuper(intptr,intptr) .cs:2983 at Xamarin.Forms.Platform.iOS.PageRenderer.get_NativeView()[0x00008] in <9a5948fff8254b13a3d593c76c73b9b1>:0 at Xamarin.Forms.Platform.iOS.PageRenderer.SetElement(Xamarin.Forms.VisualElement element)[0x0004f]在<9a5948fff8254b13a3d593c76c73b9b1>:0在Xamarin.Forms.Platform.iOS.Platform.CreateRenderer(Xamari) n.Forms.VisualElement element)[0x00014]在<9a5948fff8254b13a3d593c76c73b9b1>:0中Xamarin.Forms.Platform.iOS.NavigationRenderer.CreateViewControllerForPage(Xamarin.Forms.Page page)[0x00008]在<9a5948fff8254b13a3d593c76c73b9b1>:0在Xamarin.Forms . 位于/Library/Frameworks/Xamarin.iOS.framework/Versions/11.3中的System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()[0x0000c]中的<9a5948fff8254b13a3d593c76c73b9b1>:0中的Platform.iOS.NavigationRenderer d__45.MoveNext()[0x00016] . 0.47 / src / mono / mcs / class / referencesource / mscorlib / system / runtime / exceptionservices / exceptionservicescommon.cs:151 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task task)[0x00037] in /库/框架/ Xamarin.iOS.framework / Versions / 11.3.0.47 / src / mono / mcs / class / referencesource / mscorlib / system / runtime / compilerservices / TaskAwaiter.cs:187 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification( System.Threading.Tasks.Task任务)[0x00028 ] in/Library/Frameworks/Xamarin.iOS.framework/Versions/11.3.0.47/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task任务)[0x00008]在/Library/Frameworks/Xamarin.iOS.framework/Versions/11.3.0.47/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter .cs:128位于/Library/Frameworks/Xamarin.iOS.framework/Versions/11.3.0.47/src/mono/mcs/class/中的System.Runtime.CompilerServices.TaskAwaiter`1 [TResult] .GetResult()[0x00000] referencesource / mscorlib / system / runtime / compilerservices / TaskAwaiter.cs:357 at Xamarin.Forms.Platform.iOS.NavigationRenderer d.MoveNext()[0x00067] in <9a5948fff8254b13a3d593c76c73b9b1>:0 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw( )/Library/Frameworks/Xamarin.iOS.framework/Versions/11.3.0.47/src/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptions中的[0x0000c]在/Library/Frameworks/Xamarin.iOS.framework/Versions/11.3.0.47/src/mono/mcs中的System.Runtime.CompilerServices.AsyncMethodBuilderCore <> c.b__6_0(System.Object state)[0x00000]中的ervicescommon.cs:151 /class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1018 at UIKit.UIKitSynchronizationContext c__AnonStorey0 . <> m__0()[0x00000] / Users / builder / data / lanes / 5520 / 51128b8c / source / xamarin- macios / src / UIKit / UIKitSynchronizationContext.cs:24位于/Users/builder/data/lanes/5520/51128b8c/source/xamarin-macios/src/Foundation/NSAction.cs中的Foundation.NSAsyncActionDispatcher.Apply()[0x00000]: 163 at at(wrapper managed-to-native)UIKit.UIApplication:UIApplicationMain(int,string [],intptr,intptr)在UIKit.UIApplication.Main(System.String [] args,System.IntPtr principal,System.IntPtr委托)在UIKit.UIApplication.Main(System.String [] args,System.String中的/Users/builder/data/lanes/5520/51128b8c/source/xamarin-macios/src/UIKit/UIApplication.cs:79中的[0x00005] princi在EssentialsProMobile.iOS.Application.Main(System . String [] args)[0x00001] in /Users/odisharkins/Projects/EssentialsProMobile/iOS/Main.cs:17

这是MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:SignaturePadDemo"
             xmlns:signatures="clr-namespace:SignaturePad.Forms;assembly=SignaturePad.Forms"
             x:Class="EssentialsProMobile.MainPage"
             Title="Draw Signature">
<Grid Padding="20" RowSpacing="20">
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <signatures:SignaturePadView 
        x:Name="signaturePad"
        VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"
        Grid.Row="0" />

    <Button
        HorizontalOptions="FillAndExpand"
        Text="Show Image"
        Clicked="OnShowImage"
        Grid.Row="1" />

</Grid>

MainPage.xaml.cs中

using System;
using Xamarin.Forms;
using SignaturePad.Forms;
using PCLStorage;

namespace EssentialsProMobile
{

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
    }

    private async void OnShowImage(object sender, EventArgs e)
    {
        // get the file name
        var folder = FileSystem.Current.LocalStorage;
        var file = await folder.CreateFileAsync($"signature.jpg", CreationCollisionOption.GenerateUniqueName);

        // create some settings to control the output
        var settings = new ImageConstructionSettings
        {
            BackgroundColor = Color.White,
            StrokeColor = Color.Black,
        };

        // write the signature stream to the file stream
        using (var stream = await signaturePad.GetImageStreamAsync(SignatureImageFormat.Jpeg, settings))
        using (var fileStream = await file.OpenAsync(FileAccess.ReadAndWrite))
        {
            // copy the bytes
            await stream.CopyToAsync(fileStream);
        }

        // success
        await DisplayAlert("Signature Saved", "Signature file saved successfully.\nLoading preview...", "OK");

        // show the image preview
        var previewPage = new ContentPage
        {
            Title = "View Signature",
            Content = new Image
            {
                Source = ImageSource.FromFile(file.Path),
                Margin = new Thickness(20)
            }
        };
        await Navigation.PushAsync(previewPage);
    }
}
}

和App.xaml.cs

namespace EssentialsProMobile
{
public partial class App : Application
{
readonly Services.IAuthenticationService _AuthenticationService;

    static Application app;

    static Data.EssentialsProDatabase database;

    public static Application CurrentApp
    {
        get { return app; }
    }
    public App()
    {
        InitializeComponent();
        app = this;
        _AuthenticationService = DependencyService.Get<Services.IAuthenticationService>();

        // If the App.IsAuthenticated property is false, modally present the SplashPage.
        if (!_AuthenticationService.IsAuthenticated)
        {
            //Load Login Page
            var page = new SplashPage();
            //Testing....
            //var page = new Services.SignaturePadView();
            NavigationPage.SetHasNavigationBar(page, false);



            MainPage = new NavigationPage(new MainPage());
        }
        else
        {
            GoToRoot();
        }

回答(0)