引言
我正在创建我的第一个Xamarin应用程序(它将首先针对UWP,然后是安卓,最后可能是iOS)。
基本上,应用程序应该检测到多个手指和圆圈会弹出每一个手指并跟随它们。
我的应用程序
首先,我认为UI和图形不能用Xamarin编码,所以我启动了UWP代码:
MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Windows.Input;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Devices.Input;
using Xamarin.Forms;
using Xamarin.Forms.Platform.UWP;
using Windows.UI.Input;
using Windows.UI.Xaml.Shapes;
using Windows.UI;
namespace App1.UWP
{
public sealed partial class MainPage
{
public MainPage()
{
this.InitializeComponent();
LoadApplication(new App1.App());
}
private async void OnPointerPressed(object sender, PointerRoutedEventArgs e)
{
Debug.WriteLine(e.Pointer.PointerDeviceType + " " + sender + " " + e);
Point pointerPos = e.GetCurrentPoint(rootPanel).Position;
Debug.WriteLine(pointerPos.X + " " + pointerPos.Y);
Ellipse ellipse1 = new Ellipse();
ellipse1.Fill = new SolidColorBrush(Colors.SteelBlue);
ellipse1.Width = 100;
ellipse1.Height = 100;
//ellipse1.Margin.Left = pointerPos.X + 50;
//ellipse1.Margin.Top = pointerPos.Y + 50;
rootPanel.Children.Add(ellipse1);
Debug.WriteLine(rootPanel.Children.Count);
switch (e.Pointer.PointerDeviceType)
{
case PointerDeviceType.Touch:
break;
case PointerDeviceType.Pen:
break;
case PointerDeviceType.Mouse:
ContentDialog noMouseAvaliableDialog = new ContentDialog()
{
Title = "Erreur de méthode d'entrée",
Content = "Il est impossible de savoir qui va jouer en premier aver un joueur :(\n Veuillez essayer avec le clavier.",
PrimaryButtonText = "OK chef"
};
await noMouseAvaliableDialog.ShowAsync();
break;
default:
break;
}
Debug.WriteLine("-------------------------");
}
}
}MainPage.xaml
<forms:WindowsPage
x:Class="App1.UWP.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:forms="using:Xamarin.Forms.Platform.UWP"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1.UWP"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
PointerPressed="OnPointerPressed">
<RelativePanel
Name="rootPanel"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
</RelativePanel>
</forms:WindowsPage>正如你所看到的,我可以得到光标的X和Y位置,但是我的椭圆不显示。所以我注意到Xamarin xaml赢了,而不是我的UWP xaml。
所以我又试了一次。我寻找共享代码图形API,我找到了SkiaSharp。我的椭圆出现在Xamarin代码中:
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;
namespace App1
{
public partial class MainPage : ContentPage
{
private static SKCanvas canvas;
public MainPage()
{
InitializeComponent();
}
private void OnPainting(object sender, SKPaintSurfaceEventArgs e)
{
canvas = e.Surface.Canvas;
}
private void onTapGestureRecognizerTapped(object sender, EventArgs e)
{
Debug.WriteLine("Tapped !!!!!");
//((TappedEventArgs) e).
}
/*private void onPanUpdated(object sender, PanUpdatedEventArgs e)
{
Debug.WriteLine("Panned !!!!!" + e.TotalX + " " + e.TotalY);
}*/
internal void drawCircleOnCanvas(Point pointerPos, int radius)
{
Debug.WriteLine(pointerPos.X + " " + pointerPos.Y);
SKPaint circleFill = new SKPaint
{
IsAntialias = true,
Style = SKPaintStyle.Fill,
Color = SKColors.AliceBlue
};
canvas.DrawCircle(((float) pointerPos.X - 50), ((float) pointerPos.Y - 50), 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"
x:Class="App1.MainPage"
xmlns:views="clr-namespace:SkiaSharp.Views.Forms;assembly=SkiaSharp.Views.Forms">
<Label Text="Ooooh, you touch my tralala"
VerticalOptions="Center"
HorizontalOptions="Center" />
<!--<relativelayout
x:Name="rootPanel"
backgroundcolor="lime"
horizontaloptions="fill"
verticaloptions="fill">
</relativelayout>-->
<Grid>
<views:SKCanvasView PaintSurface="OnPainting"/>
<Grid.GestureRecognizers>
<TapGestureRecognizer
Tapped="onTapGestureRecognizerTapped"
NumberOfTapsRequired="1"/>
<!--<PanGestureRecognizer PanUpdated="onPanUpdated"/> -->
</Grid.GestureRecognizers>
</Grid>
</ContentPage>最后,它只停留在X和Y的位置,每个接触点,以画我的椭圆。如你所见,我看起来不一样:TapGestureRecognizer,PanGestureRecognizer .
但我没能得到那些坐标。
我找到了MR.Gestures API,但它是每个应用程序名称都有许可的。
问题
所以,我要问的是实验的人:
或者简单地说,,我如何才能得到所有接触点的坐标与?
谢谢
PS:我在写这堆东西的时候发现了这篇文章,我在读.
发布于 2017-04-03 15:42:08
是否提供触摸位置/手势API?
不幸的是,不!您唯一能做的就是使用TapGestureRecognizer、PinchGestureRecognizer或PanGestureRecognizer类,就像您提到的那样,但仅此而已。他们都不会给你x/y坐标。
MR.Gestures是处理触摸的最佳解决方案吗?
我遇到了同样的问题。如果您不想重新发明轮子,那么您肯定应该使用外部API。
Mr.Gestures的工作做得很好。不幸的是,我没有听说过任何免费API。正如您所提到的,这是每个应用程序许可的:
或者简单地说,我如何才能得到所有接触点的坐标与Xamarin?
您应该为每个平台实现自定义呈现器(或者在您的情况下实现UWP )。这就是Mr.Gestures在幕后为您所做的。如果您不想自己实现这一点,目前您唯一的选择是使用外部API。
也许有一天Xamarin会提供这种API .让我们保持手指交叉!
希望这能有所帮助!
发布于 2020-01-08 16:52:23
https://stackoverflow.com/questions/43186122
复制相似问题