1

I can not figure out why it is returning this error, I am beginning to think that there is some bug in Visual Studio for Mac that keeps something in memory, I have dumped the Bin and obj folders and that did not make a difference I have all my packages up to date.

Stack Trace...

System.InvalidCastException: Specified cast is not valid. at at (wrapper castclass) System.Object:__castclass_with_cache (object,intptr,intptr) at Xamarin.Forms.Internals.Registrar1[TRegistrable].GetHandler (System.Type type) [0x0001b] in D:\agent_work\2\s\Xamarin.Forms.Core\Registrar.cs:37 at Xamarin.Forms.Internals.Registrar1[TRegistrable].GetHandlerForObject[TOut] (System.Object obj) [0x0002c] in D:\agent_work\2\s\Xamarin.Forms.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] in <9a5948fff8254b13a3d593c76c73b9b1>:0 at Xamarin.Forms.Platform.iOS.VisualElementPackager.Load () [0x0001e] in <9a5948fff8254b13a3d593c76c73b9b1>:0 at Xamarin.Forms.Platform.iOS.VisualElementRenderer1[TElement].SetElement (TElement element) [0x000cc] in <9a5948fff8254b13a3d593c76c73b9b1>:0 at 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 element) [0x00014] in <9a5948fff8254b13a3d593c76c73b9b1>:0 at Xamarin.Forms.Platform.iOS.VisualElementPackager.OnChildAdded (Xamarin.Forms.VisualElement view) [0x0002a] in <9a5948fff8254b13a3d593c76c73b9b1>:0 at Xamarin.Forms.Platform.iOS.VisualElementPackager.Load () [0x0001e] in <9a5948fff8254b13a3d593c76c73b9b1>:0 at Xamarin.Forms.Platform.iOS.VisualElementRenderer1[TElement].SetElement (TElement element) [0x000cc] in <9a5948fff8254b13a3d593c76c73b9b1>:0 at 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 element) [0x00014] in <9a5948fff8254b13a3d593c76c73b9b1>:0 at Xamarin.Forms.Platform.iOS.VisualElementPackager.OnChildAdded (Xamarin.Forms.VisualElement view) [0x0002a] in <9a5948fff8254b13a3d593c76c73b9b1>:0 at Xamarin.Forms.Platform.iOS.VisualElementPackager.Load () [0x0001e] in <9a5948fff8254b13a3d593c76c73b9b1>:0 at Xamarin.Forms.Platform.iOS.PageRenderer.ViewDidLoad () [0x00086] in <9a5948fff8254b13a3d593c76c73b9b1>:0 at at (wrapper managed-to-native) ObjCRuntime.Messaging:IntPtr_objc_msgSendSuper (intptr,intptr) at UIKit.UIViewController.get_View () [0x00030] in /Users/builder/data/lanes/5520/51128b8c/source/xamarin-macios/src/build/ios/native/UIKit/UIViewController.g.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] in <9a5948fff8254b13a3d593c76c73b9b1>:0 at Xamarin.Forms.Platform.iOS.Platform.CreateRenderer (Xamarin.Forms.VisualElement element) [0x00014] in <9a5948fff8254b13a3d593c76c73b9b1>:0 at Xamarin.Forms.Platform.iOS.NavigationRenderer.CreateViewControllerForPage (Xamarin.Forms.Page page) [0x00008] in <9a5948fff8254b13a3d593c76c73b9b1>:0 at Xamarin.Forms.Platform.iOS.NavigationRenderer+d__45.MoveNext () [0x00016] in <9a5948fff8254b13a3d593c76c73b9b1>:0 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.3.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 /Library/Frameworks/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 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 task) [0x00008] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.3.0.47/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.3.0.47/src/mono/mcs/class/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 () [0x0000c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.3.0.47/src/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:151 at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.b__6_0 (System.Object state) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.3.0.47/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1018 at UIKit.UIKitSynchronizationContext+c__AnonStorey0.<>m__0 () [0x00000] in /Users/builder/data/lanes/5520/51128b8c/source/xamarin-macios/src/UIKit/UIKitSynchronizationContext.cs:24 at Foundation.NSAsyncActionDispatcher.Apply () [0x00000] in /Users/builder/data/lanes/5520/51128b8c/source/xamarin-macios/src/Foundation/NSAction.cs:163 at at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr) at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Users/builder/data/lanes/5520/51128b8c/source/xamarin-macios/src/UIKit/UIApplication.cs:79 at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Users/builder/data/lanes/5520/51128b8c/source/xamarin-macios/src/UIKit/UIApplication.cs:63 at EssentialsProMobile.iOS.Application.Main (System.String[] args) [0x00001] in /Users/odisharkins/Projects/EssentialsProMobile/iOS/Main.cs:17

This is the 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);
    }
}
}

And the 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();
        }
4
  • 1
    which line is causing the exception?
    – Jason
    Nov 29, 2017 at 2:53
  • upload this source into githhub, then its easy for someone to give help Nov 29, 2017 at 2:53
  • no, don't. You should not expect SO users to download your code, build it and debug it for you. You need to do the legwork yourself.
    – Jason
    Nov 29, 2017 at 3:30
  • Jason, the line that is causing problems is the New NavagationPage(new MainPage()) Nov 29, 2017 at 16:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.