SkiaSharp无效的强制转换异常

时间:2017-02-01 23:15:21

标签: xamarin xamarin.android xamarin.forms skiasharp

InvalidCastException中接收LoadApplication(new App());。 我在PCL和Android中添加了SkiaSharp和SkiaSharp.Views.Forms。它还自动将SkiaSharp.Views包添加到Android项目中。

我删除了所有复杂的代码并将此部分保留在我的XAML文件中

<ContentView 
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:skia="clr-namespace:SkiaSharp.Views.Forms;assembly=SkiaSharp.Views.Forms"     
    x:Class="SkiaSharpRemote.MediaControlView">
    <ContentView.Content>
        <!--<skia:SKCanvasView
            x:Name="canvas"
            VerticalOptions="Fill"
            HorizontalOptions="Fill"
            PaintSurface="Canvas_PaintSurface">
        </skia:SKCanvasView>-->
    </ContentView.Content>
</ContentView>

在我评论skCanvasView行之后加载了该应用。如果我取消注释它,则抛出以下异常。

  

at(wrapper castclass)System.Object:__ castclass_with_cache   (object,intptr,intptr)at   Xamarin.Forms.Registrar 1[TRegistrable].GetHandler (System.Type type) [0x0001b] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Core\Registrar.cs:24 at Xamarin.Forms.Registrar 1 [TRegistrable] .GetHandler [TOUT]   (System.Type类型)[0x00000] in   C:\ BuildAgent2 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Core \ Registrar.cs:29   在Xamarin.Forms.Platform.Android.Platform.CreateRenderer   (Xamarin.Forms.VisualElement元素)[0x00006] in   C:\ BuildAgent2 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \ Platform.cs:288   在Xamarin.Forms.Platform.Android.VisualElementPackager.AddChild   (Xamarin.Forms.VisualElement视图,   Xamarin.Forms.Platform.Android.IVisualElementRenderer oldRenderer,   Xamarin.Forms.Platform.Android.RendererPool池,System.Boolean   sameChildren)[0x00023] in   C:\ BuildAgent2 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \ VisualElementPackager.cs:74   在Xamarin.Forms.Platform.Android.VisualElementPackager.SetElement   (Xamarin.Forms.VisualElement oldElement,Xamarin.Forms.VisualElement   newElement)[0x00104] in   C:\ BuildAgent2 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \ VisualElementPackager.cs:207   在Xamarin.Forms.Platform.Android.VisualElementPackager.Load()   [0x00000] in   C:\ BuildAgent2 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \ VisualElementPackager.cs:58   在   Xamarin.Forms.Platform.Android.VisualElementRenderer 1[TElement].SetPackager (Xamarin.Forms.Platform.Android.VisualElementPackager packager) [0x00007] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:337 at Xamarin.Forms.Platform.Android.VisualElementRenderer 1 [TElement] .SetElement   (元素元素)[0x00111] in   C:\ BuildAgent2 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \ VisualElementRenderer.cs:196   在   Xamarin.Forms.Platform.Android.VisualElementRenderer 1[TElement].Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00027] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:129 at Xamarin.Forms.Platform.Android.Platform.CreateRenderer (Xamarin.Forms.VisualElement element) [0x0001f] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\Platform.cs:289 at Xamarin.Forms.Platform.Android.VisualElementPackager.AddChild (Xamarin.Forms.VisualElement view, Xamarin.Forms.Platform.Android.IVisualElementRenderer oldRenderer, Xamarin.Forms.Platform.Android.RendererPool pool, System.Boolean sameChildren) [0x00023] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:74 at Xamarin.Forms.Platform.Android.VisualElementPackager.SetElement (Xamarin.Forms.VisualElement oldElement, Xamarin.Forms.VisualElement newElement) [0x00104] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:207 at Xamarin.Forms.Platform.Android.VisualElementPackager.Load () [0x00000] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:58 at Xamarin.Forms.Platform.Android.VisualElementRenderer 1 [TElement] .SetPackager   (Xamarin.Forms.Platform.Android.VisualElementPackager packager)   [0x00007] in   C:\ BuildAgent2 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \ VisualElementRenderer.cs:337   在   Xamarin.Forms.Platform.Android.VisualElementRenderer 1[TElement].SetElement (TElement element) [0x00111] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:196 at Xamarin.Forms.Platform.Android.VisualElementRenderer 1 [TElement] .Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement   (Xamarin.Forms.VisualElement元素)[0x00027] in   C:\ BuildAgent2 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \ VisualElementRenderer.cs:129   在Xamarin.Forms.Platform.Android.Platform.CreateRenderer   (Xamarin.Forms.VisualElement元素)[0x0001f] in   C:\ BuildAgent2 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \ Platform.cs:289   在Xamarin.Forms.Platform.Android.VisualElementPackager.AddChild   (Xamarin.Forms.VisualElement视图,   Xamarin.Forms.Platform.Android.IVisualElementRenderer oldRenderer,   Xamarin.Forms.Platform.Android.RendererPool池,System.Boolean   sameChildren)[0x00023] in   C:\ BuildAgent2 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \ VisualElementPackager.cs:74   在Xamarin.Forms.Platform.Android.VisualElementPackager.SetElement   (Xamarin.Forms.VisualElement oldElement,Xamarin.Forms.VisualElement   newElement)[0x00104] in   C:\ BuildAgent2 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \ VisualElementPackager.cs:207   在Xamarin.Forms.Platform.Android.VisualElementPackager.Load()   [0x00000] in   C:\ BuildAgent2 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \ VisualElementPackager.cs:58   在   Xamarin.Forms.Platform.Android.VisualElementRenderer 1[TElement].SetPackager (Xamarin.Forms.Platform.Android.VisualElementPackager packager) [0x00007] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:337 at Xamarin.Forms.Platform.Android.VisualElementRenderer 1 [TElement] .SetElement   (元素元素)[0x00111] in   C:\ BuildAgent2 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \ VisualElementRenderer.cs:196   在   Xamarin.Forms.Platform.Android.VisualElementRenderer`1 [TElement] .Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement   (Xamarin.Forms.VisualElement元素)[0x00027] in   C:\ BuildAgent2 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \ VisualElementRenderer.cs:129   在Xamarin.Forms.Platform.Android.Platform.CreateRenderer   (Xamarin.Forms.VisualElement元素)[0x0001f] in   C:\ BuildAgent2 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \ Platform.cs:289   在Xamarin.Forms.Platform.Android.AppCompat.Platform.AddChild   (Xamarin.Forms.Page页面,System.Boolean布局)[0x00015] in   C:\ BuildAgent2 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \程序兼容性\ Platform.cs:250   在Xamarin.Forms.Platform.Android.AppCompat.Platform.SetPage   (Xamarin.Forms.Page newRoot)[0x00089] in   C:\ BuildAgent2 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \程序兼容性\ Platform.cs:239   在   Xamarin.Forms.Platform.Android.FormsAppCompatActivity.InternalSetPage   (Xamarin.Forms.Page页面)[0x0009b] in   C:\ BuildAgent2 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \程序兼容性\ FormsAppCompatActivity.cs:374   在Xamarin.Forms.Platform.Android.FormsAppCompatActivity.SetMainPage   ()[0x00000] in   C:\ BuildAgent2 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \程序兼容性\ FormsAppCompatActivity.cs:444   在   Xamarin.Forms.Platform.Android.FormsAppCompatActivity.LoadApplication   (Xamarin.Forms.Application应用程序)[0x00183] in   C:\ BuildAgent2 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \程序兼容性\ FormsAppCompatActivity.cs:135   在SkiaSharpRemote.Droid.MainActivity.OnCreate(Android.OS.Bundle)   捆绑)[0x0002e] in   /Users/simonlam/Desktop/Samples/Xamarin/SkiaRemote/SkiaSharpRemote/Droid/MainActivity.cs:25   在   Android.Support.V4.App.FragmentActivity.n_OnCreate_Landroid_os_Bundle_   (System.IntPtr jnienv,System.IntPtr native__this,System.IntPtr   native_savedInstanceState)[0x00011] in   &lt; 27c17fe440cf491ba8255bcefade6e02&gt;:0 at(包装器动态方法)   System.Object的:a050e315-9322-4894-b9ce-8db88ad775ab   (IntPtr的,IntPtr的,IntPtr的)

错误并没有告诉我多少,我只看到这是某种投射错误。

我还有另一个使用过skiasharp的项目。我基本上遵循相同的路线,但对于这个项目,它没有工作。

1 个答案:

答案 0 :(得分:5)

执行以下操作:

  1. 将SkiaSharp.Views.Forms NuGet添加到所有项目中! (PCL /共享, Android和iOS项目。)
  2. 然后清理并重建您的解决方案。
  3. 这将解决您的问题。