我们的页面需要渐变,所以我们使用 SkiaSharp 和 GradientView。这在 Android 上运行良好,但在 iOS 上不起作用 - Gradient 只是不显示。
我们是否需要使用 AppDelegate 中的渲染器或代码对 iOS 上的 SkiaSharp 进行任何特定于 iOS 的初始化?
编辑这是在装有 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);
}
}
}