Reputation: 949
We need a Gradient on our pages, so we're using SkiaSharp and a GradientView. This works fine on Android but isn't working on iOS - the Gradient just doesn't display.
Do we need to do any iOS-specific initialisation for SkiaSharp on iOS, with renderers or code in the AppDelegate?
Edit This is running on an iPad with iOS 10.3.3. Our 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>
Our 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);
}
}
}
Upvotes: 0
Views: 978
Reputation: 21
I ran into the same problem and read the documentation. It says you need to tell the canvas to update. You do this using the following line of code:
canvasView.InvalidateSurface();
Upvotes: 1
Reputation: 5222
Maybe you haven't installed the NuGets (both SkiaSharp and SkiaSharp.Views.Forms) into the iOS project?
I copy-pasted your code into a brand new app, and it worked first time.
Some other things that may be the issue is that the view height/width is 0. Or, the dodgy case where the color is transparent.
Upvotes: 0