在尝试了几个模板之后,我终于找到了正确的模板。
步骤1:
第2步:这非常重要。请注意,单选按钮已更改为“.NET Standard”。
第 3 步:现在我们可以添加依赖项,添加 SkiaSharp
和
MainPage.xml 应如下所示:
<?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:Test2"
x:Class="Test2.MainPage">
<skia:SKCanvasView x:Name="CanvasView"
PaintSurface="OnCanvasViewPaintSurface" />
</ContentPage>
后面的代码是:
using SkiaSharp;
using SkiaSharp.Views.Forms;
using Xamarin.Forms;
namespace Test2 {
public partial class MainPage : ContentPage {
public static readonly SKCanvasView canvasView = new SKCanvasView();
public MainPage() {
InitializeComponent();
canvasView.PaintSurface += OnCanvasViewPaintSurface;
Content = canvasView;
}
private void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs e) {
var surface = e.Surface;
var canvas = surface.Canvas;
var width = e.Info.Width;
var height = e.Info.Height;
var x = width/2.0f;
var y = height/2.0f;
var paint = new SKPaint();
paint.TextSize = 14.0f;
paint.IsAntialias = true;
paint.Color = SKColors.Red;
paint.IsStroke = false;
var textBounds = new SKRect();
var text = "Welcome to SkiaSharp";
paint.MeasureText(text, ref textBounds);
var textWidth = textBounds.Width + 4.0f;
var textHeight = textBounds.Height + 2.0f;
x -= textWidth/2.0f;
canvas.DrawText(text, x, y, paint);
}
}
}
When select the Test2.UWP to be the start-up project, you will then see:
现在是喝啤酒的时候了!