当 canvas 位于 ItemTemplate 内时如何调用 InvalidateSurface 方法?

How can I call InvalidateSurface method when canvas is inside an ItemTemplate?

我希望能够更新在 SKCanvasView 上绘制的图像。 canvas 包含在 Telerik SlideView 项目模板中。下面是我如何将图像绘制到 canvas:

private void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
{
    SKImageInfo info = args.Info;
    SKSurface surface = args.Surface;
    SKCanvas canvas = surface.Canvas;

    canvas.Clear();
    float x = info.Width / 2 - _currentBitmap.Width / 2;
    float y = info.Height / 2 - _currentBitmap.Height / 2;

    if (sender is BindableObject bo)
    {
        if (bo.BindingContext is MyObjectType o)
        {
            string file = o.Id + ".jpg";
            if (File.Exists(file))
            {
                _currentBitmap = SKBitmap.Decode(file);
                canvas.DrawBitmap(_currentBitmap, x, y);
            }
        }
    }
}

我需要在触发其他事件后更改绘制的图像。我知道要刷新 SKCanvasView,我调用了 InvalidateSurface() 方法。这适用于我的 onTouch 事件:

private void Canvas_OnTouch(object sender, SKTouchEventArgs e)
{
    if (e == null) return;
    if (e.ActionType.Equals(SKTouchAction.Pressed))
    {
        if (sender is SKCanvasView canvas)
        {
            canvas.InvalidateSurface();
        }
    }
}

但是,我还需要在代码中的其他位置更改绘制的图像。我怎样才能在其他方法中访问 SKCanvasView 以便调用 InvalidateSurface()? OnCanvasViewPaintSurface 方法似乎只公开了 SKCanvas 而不是 SKCanvasView,所以我不能为它设置一个 class 成员并从其他方法访问它。

我自己设法解决了这个问题。 OnTouch 事件中的 sender 对象参数实际上是 SKCanvasView 类型,所以你可以设置一个 class 属性 来引用它。