安装SkiaSharp后无法运行Xamarin项目?

时间:2017-12-06 16:44:28

标签: c# xamarin.forms skiasharp

我安装了SkiaSharpSkiaSharp.Views.Forms 当我在我的代码中使用它们时:

public MainPage()
        {
            InitializeComponent();

            SKCanvasView canvasView = new SKCanvasView();
            canvasView.PaintSurface += OnCanvasViewPaintSurface;
            Content = canvasView;

        }


void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
    {
        SKImageInfo info = args.Info;
        SKSurface surface = args.Surface;
        SKCanvas canvas = surface.Canvas;

        canvas.Clear();

        SKPaint paint = new SKPaint
        {
            Style = SKPaintStyle.Stroke,
            Color = Color.Red.ToSKColor(),
            StrokeWidth = 25
        };
        canvas.DrawCircle(info.Width / 2, info.Height / 2, 100, paint);

        paint.Style = SKPaintStyle.Fill;
        paint.Color = SKColors.Blue;
        canvas.DrawCircle(info.Width / 2, info.Height / 2, 100, paint);
    }

项目无法运行,(调试模式的橙色立即消失),并且没有错误。

当我注释掉上面的代码时,项目正常运行! 我正在使用Visual Studio 2017版本15.4.5

我尝试在VS 2015中打开项目,我在Android项目的MainActivity中遇到了这个例外:

LoadApplication(new App());

消息:

  

指定的演员表无效。

堆栈跟踪:

  

System.InvalidCastException:指定的强制转换无效。在(包装   castclass)System.Object:__ castclass_with_cache(object,intptr,intptr)   在Xamarin.Forms.Internals.Registrar1 [TRegistrable] .GetHandler   (System.Type类型)[0x0001b] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Core \ Registrar.cs:37   在Xamarin.Forms.Internals.Registrar1 [TRegistrable] .GetHandler [TOut]   (System.Type类型)[0x00000] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Core \ Registrar.cs:42   在Xamarin.Forms.Platform.Android.Platform.CreateRenderer   (Xamarin.Forms.VisualElement元素)[0x00006] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \ Platform.cs:284   在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:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \ VisualElementPackager.cs:86   在Xamarin.Forms.Platform.Android.VisualElementPackager.SetElement   (Xamarin.Forms.VisualElement oldElement,Xamarin.Forms.VisualElement   newElement)[0x00104] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \ VisualElementPackager.cs:219   在Xamarin.Forms.Platform.Android.VisualElementPackager.Load()   [0x00000] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \ VisualElementPackager.cs:70   在   Xamarin.Forms.Platform.Android.VisualElementRenderer1 [TElement] .SetPackager   (Xamarin.Forms.Platform.Android.VisualElementPackager packager)   [0x00007] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \ VisualElementRenderer.cs:371   在   Xamarin.Forms.Platform.Android.VisualElementRenderer1 [TElement] .SetElement   (元素元素)[0x000ce] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \ VisualElementRenderer.cs:174   在   Xamarin.Forms.Platform.Android.VisualElementRenderer`1 [TElement] .Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement   (Xamarin.Forms.VisualElement元素)[0x00027] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \ VisualElementRenderer.cs:120   在Xamarin.Forms.Platform.Android.Platform.CreateRenderer   (Xamarin.Forms.VisualElement元素)[0x0001f] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \ Platform.cs:285   在Xamarin.Forms.Platform.Android.AppCompat.Platform.AddChild   (Xamarin.Forms.Page页面,System.Boolean布局)[0x00015] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \程序兼容性\ Platform.cs:268   在Xamarin.Forms.Platform.Android.AppCompat.Platform.SetPage   (Xamarin.Forms.Page newRoot)[0x0007a] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \程序兼容性\ Platform.cs:245   在   Xamarin.Forms.Platform.Android.FormsAppCompatActivity.InternalSetPage   (Xamarin.Forms.Page页面)[0x0009b] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \程序兼容性\ FormsAppCompatActivity.cs:337   在Xamarin.Forms.Platform.Android.FormsAppCompatActivity.SetMainPage   ()[0x00000] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \程序兼容性\ FormsAppCompatActivity.cs:407   在   Xamarin.Forms.Platform.Android.FormsAppCompatActivity.LoadApplication   (Xamarin.Forms.Application应用程序)[0x00229] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \程序兼容性\ FormsAppCompatActivity.cs:152   在XamApp.Droid.MainActivity.OnCreate(Android.OS.Bundle包)   [0x00028]在D:\ P \ XamApp \ XamApp \ XamApp.Android \ MainActivity.cs:25

1 个答案:

答案 0 :(得分:0)

要使用,我必须通过nuget将SkiaSharpSkiaSharp.ViewsSkiaSharp.Views.Forms安装到Android和iOS项目中,PCL只需要SkiaSharp和{{1} }。

原始答案在这里: https://forums.xamarin.com/discussion/comment/310695/#Comment_310695