Xamarin.Forms SkiaSharp Gradient在Android上运行,但在iOS上运行

我们需要在页面上使用渐变,因此我们使用的是SkiaSharp和GradientView . 这在Android上工作正常,但在iOS上无效 - Gradient不显示 .

我们是否需要在iOS上对SkiaSharp进行任何iOS特定的初始化,在AppDelegate中使用渲染器或代码?

Edit 这是在iOS 10.3.3的iPad上运行的 . 我们的XAML:

<?xml version="1.0" encoding="utf-8" ?>
    <ContentView xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="MyProject.XForms.Controls.GradientView">
    </ContentView>

我们的XAML.cs

using SkiaSharp;
using SkiaSharp.Views.Forms;
using Xamarin.Forms;

namespace MyProject.XForms.Controls
{
    public partial class GradientView : ContentView
    {
        public Color StartColor { get; set; } = Color.Transparent;
        public Color EndColor { get; set; } = Color.Transparent;
        public bool Horizontal { get; set; } = false;

        public GradientView()
        {
            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();

            var colors = new SKColor[] { StartColor.ToSKColor(), EndColor.ToSKColor() };
            SKPoint startPoint = new SKPoint(0, 0);
            SKPoint endPoint = Horizontal ? new SKPoint(info.Width, 0) : new SKPoint(0, info.Height);

            var shader = SKShader.CreateLinearGradient(startPoint, endPoint, colors, null, SKShaderTileMode.Clamp);

            SKPaint paint = new SKPaint
            {
                Style = SKPaintStyle.Fill,
                Shader = shader
            };

            canvas.DrawRect(new SKRect(0, 0, info.Width, info.Height), paint);
        }
    }
}

回答(2)

2 years ago

也许您还没有将NuGets(SkiaSharp和SkiaSharp.Views.Forms)安装到iOS项目中?

我将您的代码复制粘贴到一个全新的应用程序中,并且它第一次运行 .

可能存在的其他一些问题是视图高度/宽度为0.或者,颜色是透明的狡猾的情况 .

2 years ago

我遇到了同样的问题并阅读了文档 . 它说你需要告诉画布更新 . 您可以使用以下代码行执行此操作:

canvasView.InvalidateSurface();