【发布时间】:2020-10-15 03:14:24
【问题描述】:
大家好,我使用了本教程 (https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/graphics/skiasharp/paths/finger-paint) 中的代码并使用此论坛帖子 (https://forums.xamarin.com/discussion/178190/how-to-fix-cannot-convert-from-touchtracking-touchtrackingpoint-to-xamarin-forms-point) 进行了修改,以进行屏幕绘图。下面是我的 Xamarin 的 C# 代码:
using System;
using System.Collections.Generic;
using SkiaSharp;
using SkiaSharp.Views.Forms;
using TouchTracking;
using Xamarin.Forms;
namespace MathKumu.Pages
{
public partial class WorkPage : ContentPage
{
Dictionary<long, SKPath> inProgressPaths = new Dictionary<long, SKPath>();
List<SKPath> completedPaths = new List<SKPath>();
SKPaint paint = new SKPaint
{
Style = SKPaintStyle.Stroke,
Color = SKColors.Black,
StrokeWidth = 10,
StrokeCap = SKStrokeCap.Round,
StrokeJoin = SKStrokeJoin.Round
};
public WorkPage(TopicsData topic)
{
InitializeComponent();
Title = topic.MathType;
}
void OnTouchEffectAction(object sender, TouchActionEventArgs args)
{
switch (args.Type)
{
case TouchActionType.Pressed:
if (!inProgressPaths.ContainsKey(args.Id))
{
SKPath path = new SKPath();
//path.MoveTo(ConvertToPixel(args.Location));
path.MoveTo(ConvertToPixel(new Point(args.Location.X, args.Location.Y)));
inProgressPaths.Add(args.Id, path);
canvasView.InvalidateSurface();
}
break;
case TouchActionType.Moved:
if (inProgressPaths.ContainsKey(args.Id))
{
SKPath path = inProgressPaths[args.Id];
//path.LineTo(ConvertToPixel(args.Location));
path.MoveTo(ConvertToPixel(new Point(args.Location.X, args.Location.Y)));
canvasView.InvalidateSurface();
}
break;
case TouchActionType.Released:
if (inProgressPaths.ContainsKey(args.Id))
{
completedPaths.Add(inProgressPaths[args.Id]);
inProgressPaths.Remove(args.Id);
canvasView.InvalidateSurface();
}
break;
case TouchActionType.Cancelled:
if (inProgressPaths.ContainsKey(args.Id))
{
inProgressPaths.Remove(args.Id);
canvasView.InvalidateSurface();
}
break;
}
}
//SKPoint ConvertToPixel(TouchTrackingPoint location)
//{
// return new SKPoint((float)(canvasView.CanvasSize.Width * location.X / canvasView.Width),
// (float)(canvasView.CanvasSize.Height * location.Y / canvasView.Height));
//}
SKPoint ConvertToPixel(Point pt)
{
return new SKPoint((float)(canvasView.CanvasSize.Width * pt.X / canvasView.Width),
(float)(canvasView.CanvasSize.Height * pt.Y / canvasView.Height));
}
void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
{
SKCanvas canvas = args.Surface.Canvas;
canvas.Clear();
foreach (SKPath path in completedPaths)
{
canvas.DrawPath(path, paint);
}
foreach (SKPath path in inProgressPaths.Values)
{
canvas.DrawPath(path, paint);
}
}
}
}
这是我的 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:skia="clr-namespace:SkiaSharp.Views.Forms;assembly=SkiaSharp.Views.Forms"
xmlns:tt="clr-namespace:TouchTracking.Forms;assembly=TouchTracking.Forms"
x:Class="MathKumu.Pages.WorkPage">
<ContentPage.Content>
<Grid BackgroundColor="White">
<skia:SKCanvasView x:Name="canvasView"
PaintSurface="OnCanvasViewPaintSurface" />
<Grid.Effects>
<tt:TouchEffect Capture="True"
TouchAction="OnTouchEffectAction" />
</Grid.Effects>
</Grid>
</ContentPage.Content>
</ContentPage>
在 iPhone 模拟器上,它显示画布,但无法绘制。你们能帮我解决这个问题吗?谢谢。
【问题讨论】:
-
您是否为 TouchEffect 实现了特定于平台的代码?
-
@Jason 我使用了 NuGet 包 TouchTracking。 “TouchEffect 的平台特定代码”是什么意思?谢谢。
-
TouchEffect.cs 和 TouchRecognizer.cs,如下所示:github.com/xamarin/xamarin-forms-samples/tree/master/Effects/…。我不熟悉 nuget 包,它可能会做同样的事情。
-
@Jason 我尝试使用演示进行命名空间触摸跟踪的方式,所有以 touch 开头的 c 锐利类以及放置 toucheffect.cs 和 touchrecognizer.cs 。我还删除了 nuget 包 touchtracking,因为它与他们在演示中制作的 touchtracking 命名空间有冲突。我的代码现在出现错误:XLS0419:未定义的 CLR 命名空间。 “clr-namespace” URI 引用了无法找到的命名空间“TouchTracking”。你能帮我解决这个问题吗?谢谢。
-
@Jason 为了让它指向触摸类所在的实际命名空间,我对我的项目进行了清理,现在它可以工作了。非常感谢您的帮助,我一定会更深入地回顾一下触摸类的工作原理。
标签: c# xaml xamarin xamarin.forms skiasharp