在SKCanvas

时间:2017-04-06 19:41:40

标签: c# xamarin access-violation skiasharp mr.gestures

简介

我正在创建我的第一个Xamarin应用程序(首先是针对UWP,然后是Android,最后可能是iOS)。

基本上,应用程序应检测多个手指,并且每个手指都会弹出圆圈并跟随它们。

问题

我尝试使用MR.gestures上的System.AccessViolationException方法上的staic SKCanvas绘制onPanning

截图

AccessViolationException on UWP app and Visual Studio 2017

详细

  

L'exception System.AccessViolationException s'est produite
  HResult = 0x80004003消息=尝试读取或写入受保护   记忆。这通常表明其他记忆已损坏   来源= Arborescence   des appelsdeprocédure:à   SkiaSharp.SkiaApi.sk_canvas_draw_circle(IntPtr t,Single cx,Single   cy,单半径,IntPtr paint)à   SkiaSharp.SKCanvas.DrawCircle(Single cx,Single cy,Single radius,   SKPaint paint)àApp1.MainPage.drawCircleOnCanvas(Point pointerPos,   Int32 radius)dans d:\ Profiles \ qpollet \ Documents \ Visual Studio   2017 \ Projects \ App1 \ App1 \ App1 \ MainPage.xaml.cs:ligne70à   App1.MainPage.onPanning(Object sender,PanEventArgs e)dans   d:\ Profiles \ qpollet \ Documents \ Visual Studio   2017 \ Projects \ App1 \ App1 \ App1 \ MainPage.xaml.cs:ligne54à   MR.Gestures.GestureHandler<> c__DisplayClass115_0`1.b__0()   一个   Xamarin.Forms.Platform.UWP.WindowsBasePlatformServices<> c__DisplayClass2_0.b__0()

代码

MainPage.xaml.cs中

using SkiaSharp;
using SkiaSharp.Views.Forms;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using MR.Gestures;

namespace App1
{
    public partial class MainPage : Xamarin.Forms.ContentPage
    {
        private static SKCanvas canvas;

        public MainPage()
        {
            InitializeComponent();
        }

        private void OnPainting(object sender, SKPaintSurfaceEventArgs e)
        {
            canvas = e.Surface.Canvas;

            Point pointerPos = new Point
            {
                X = 200,
                Y = 400
            };
            drawCircleOnCanvas(pointerPos, 50);

            Point pointerPos1 = new Point
            {
                X = 1000,
                Y = 600
            };
            drawCircleOnCanvas(pointerPos1, 50);
        }

        private void onPanning(object sender, PanEventArgs e)
        {
            Debug.WriteLine("MR Panning !!!!!");

            try
            {
                List<Point> touches = e.Touches.ToList();
                //foreach (Point touch in touches)
                for(int i = 0; i <= touches.Count; i++)
                {
                    Point touch = touches[i];
                    Debug.WriteLine("index " + i + " : " + touch.X + " " + touch.Y);
                    drawCircleOnCanvas(touch, 50);
                }
            }
            catch (ArgumentNullException) { }
        }

        internal void drawCircleOnCanvas(Point pointerPos, int radius)
        {
            Debug.WriteLine(pointerPos.X + " " + pointerPos.Y);

            SKPaint circleFill = new SKPaint
            {
                IsAntialias = true,
                Style = SKPaintStyle.Fill,
                Color = SKColors.Red
            };
            canvas.DrawCircle(((float) pointerPos.X - radius), ((float) pointerPos.Y - radius), radius, circleFill);
        }
    }
}

MainPage.xaml中

<?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:local="clr-namespace:App1"
             xmlns:views="clr-namespace:SkiaSharp.Views.Forms;assembly=SkiaSharp.Views.Forms"
             xmlns:mr="clr-namespace:MR.Gestures;assembly=MR.Gestures"
             x:Class="App1.MainPage">

    <Label Text="Ooooh, you touch my tralala" 
           VerticalOptions="Center" 
           HorizontalOptions="Center" />

    <RelativeLayout>

        <mr:AbsoluteLayout
            RelativeLayout.XConstraint="{ConstraintExpression Type=Constant, Constant=0}"
            RelativeLayout.YConstraint="{ConstraintExpression Type=Constant, Constant=0}"
            RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width}"
            RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height}"

            BackgroundColor="Blue"

            x:Name="touchLayout"
            Panning="onPanning">

        </mr:AbsoluteLayout>

        <views:SKCanvasView
            RelativeLayout.XConstraint="{ConstraintExpression Type=Constant, Constant=0}"
            RelativeLayout.YConstraint="{ConstraintExpression Type=Constant, Constant=0}"
            RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width}"
            RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height}"

            PaintSurface="OnPainting"/>

    </RelativeLayout>
</ContentPage>

结果 Xamarin SKCanvas screenshot on Windows 10 emulator

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您将收到该错误,因为您正在保存画布。画布在每个帧渲染时重新创建,因此您无法保持它的句柄。

正确的方法是只在画面中引用画布INSIDE。然后,在您的填充方法中,只需使用InvalidateSurface使表面无效。

这将触发重绘,然后您可以在新位置绘制项目。

class MainPage {
    List<Point> touches;
    void onPan() {
        touches = GetTouchPoints();
        view.InvalidateSurface();
    }
    void onPaint() {
        var canvas = ...;
        canvas.Draw(...);
    }
}