0

在 Xamarin Forms 的选项卡式页面的一个子页面中使用 SkiaSharp API 绘制圆圈时,我遇到了一些问题。

尽管在项目构建期间我没有收到任何错误,但每当我进入设备上调试期间应该在其中绘制圆圈的页面时,应用程序都会抛出“System.InvalidCastException:指定的强制转换无效”消息并且它崩溃。

这是选项卡式页面的 XAML 代码:

<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage  xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:me="clr-namespace:TestBth;assembly=TestBth"
             x:Class="TestBth.MyTabbedPage">

    <TabbedPage.Children>

        <me:ConnectPage />
        <me:LissajousPage />
        <me:ParametersPage />

    </TabbedPage.Children>

    <!--Pages can be added as references or inline-->


</TabbedPage>

这是导致问题的页面的 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:skia="clr-namespace:SkiaSharp.Views.Forms;assembly=SkiaSharp.Views.Forms"
             xmlns:local="clr-namespace:TestBth"
             x:Class="TestBth.LissajousPage"
             Title="Lissajous">

            <skia:SKCanvasView x:Name="canvasView"
                               PaintSurface="canvasView_PaintSurface" />

</ContentPage>

这是同一页面的内部代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

using SkiaSharp;
using SkiaSharp.Views.Forms;

namespace TestBth
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class LissajousPage : ContentPage
    {

        SKPaint blackFillPaint = new SKPaint
        {
            Style = SKPaintStyle.Fill,
            Color = SKColors.Black
        };

        public LissajousPage()
        {
            InitializeComponent();
        }


        private void canvasView_PaintSurface(object sender, SKPaintSurfaceEventArgs e)
        {
            SKSurface surface = e.Surface;
            SKCanvas canvas = surface.Canvas;

            canvas.Clear(SKColors.CornflowerBlue);

            int width = e.Info.Width;
            int height = e.Info.Height;

            //Set transforms
            canvas.Translate(width / 2, height / 2);
            canvas.Scale(width / 200f);

            //Clock Background
            canvas.DrawCircle(0, 0, 100, blackFillPaint);
        }

    }
}

关于为什么会发生这种情况的任何想法?

提前致谢。

4

1 回答 1

1

令我惊讶的是,事实证明 SkiaSharp.Views.Forms Nuget 包没有为 Android 安装。

我指定我想在我的所有项目中安装它,但在安装过程中一定有问题。

我为 Android 单独安装了该软件包,现在可以正常工作了。

带来不便敬请谅解。

于 2018-04-17T13:57:16.367 回答