Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 当画布位于ItemTemplate内时,如何调用InvalidateSurface方法?_Android_Xamarin.forms_Skiasharp - Fatal编程技术网

Android 当画布位于ItemTemplate内时,如何调用InvalidateSurface方法?

Android 当画布位于ItemTemplate内时,如何调用InvalidateSurface方法?,android,xamarin.forms,skiasharp,Android,Xamarin.forms,Skiasharp,我希望能够更新在SKCanvasView上绘制的图像。画布包含在Telerik SlideView项目模板中。以下是我如何在画布上绘制图像: private void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args) { SKImageInfo info = args.Info; SKSurface surface = args.Surface; SKCanvas canvas =

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

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,因此我无法将类成员设置为该类并从其他方法访问它。

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

您是否尝试使用反射?答案一直就在我面前。OnTouch事件中的sender对象参数实际上是SKCanvasView类型,因此我可以设置一个类属性来引用它。好的,请发布您的答案以结束此案例。我通常也会这样做。接受这个答案可能是个好主意。