您可以利用自定义绘图操作 API 直接访问渲染线程上的 SKCanvas。
public class CustomSkiaPage : Control
{
public CustomSkiaPage()
{
ClipToBounds = true;
}
class CustomDrawOp : ICustomDrawOperation
{
public CustomDrawOp(Rect bounds)
{
Bounds = bounds;
}
public void Dispose()
{
// No-op in this example
}
public Rect Bounds { get; }
public bool HitTest(Point p) => false;
public bool Equals(ICustomDrawOperation other) => false;
public void Render(IDrawingContextImpl context)
{
var canvas = (context as ISkiaDrawingContextImpl)?.SkCanvas;
// draw stuff
}
}
public override void Render(DrawingContext context)
{
context.Custom(new CustomDrawOp(new Rect(0, 0, Bounds.Width, Bounds.Height)));
Dispatcher.UIThread.InvokeAsync(InvalidateVisual, DispatcherPriority.Background);
}
}