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

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

回答(1)

2 years ago

为了工作,我必须通过nuget将 SkiaSharpSkiaSharp.ViewsSkiaSharp.Views.Forms 安装到Android和iOS项目中,PCL只需要 SkiaSharpSkiaSharp.Views.Forms .

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