Xamarin.Forms-在背景线程中绘制SkiaSharp

时间:2019-07-09 10:23:57

标签: c# multithreading xamarin.forms skiasharp

我正在开发Xamarin Forms应用程序。我想使用SkiaSharp库绘制一系列直线和点。可以在后台线程上绘制吗?

必须从用户界面线程中调用Microsft docs中的SKCanvasView.InvalidateSurface()方法。

2 个答案:

答案 0 :(得分:0)

您可以像这样在并行任务中添加代码

公共任务MyBeautifulPicture(){

Task.run((n)=> {

SKCanvas.DrawRect(etc,etc); })。ConfigureAwait(true);

}

答案 1 :(得分:0)

我知道这是一个旧线程,但是无论如何。

您可以从backgroundthread中执行的操作使画布无效。

XAML:
<skia:SKCanvasView x:Name="canvas" PaintSurface="OnCanvasViewPaintSurfaceAsync" />

CODE:
SKPaintSurfaceEventArgs args;

// Update canvas through background task    
async void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
    // Invalidating surface due to data change
    canvas?.InvalidateSurface();
}

async void OnCanvasViewPaintSurfaceAsync(object sender, SKPaintSurfaceEventArgs args1)
{
    args = args1;
    await drawAsync();
}

public async Task drawAsync()
{

     SKImageInfo info = args.Info;
     SKSurface surface = args.Surface;
     SKCanvas canvas = surface.Canvas;
}