Xamarin:如何获得光标/触摸坐标(位置X和Y)?

时间:2017-04-03 13:36:34

标签: c# xamarin xamarin.forms skiasharp mr.gestures

简介

我正在创建我的第一个Xamarin应用程序(首先是针对UWP,然后是Android,最后可能是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。

所以我再次尝试使用Xamarin。 我找了共享代码图形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位置上绘制我的椭圆。 如您所见,我看了不同的方式:TapGestureRecognizerPanGestureRecognizer ...

但我无法得到那些协调员。

我找到了MR.Gestures API,但它的许可每个应用名称。

问题

所以,我问实验的人:

  • Xamarin是否提供触摸位置/手势API?
  • MR.Gestures是 处理触摸的最佳解决方案?
  • 是否有其他触摸共享代码 API?

或者简单地说,如何通过Xamarin 获得所有触摸点的协调点?

由于

PS:我在写这个堆栈时找到this article,正在阅读......

2 个答案:

答案 0 :(得分:3)

  

Xamarin是否提供触摸位置/手势API?

不幸的是,不!您可以使用Xamarin做的唯一事情就是使用TapGestureRecognizerPinchGestureRecognizerPanGestureRecognizer类,如您所提到的那样,但这就是全部。他们都不会给你x / y坐标。

  

MR.Gestures是处理触摸的最佳解决方案吗?

我遇到了同样类型的问题。如果您不想重新发明轮子,那么您一定要选择外部API。

Mr.Gestures做得很好。不幸的是,我还没有听说过任何免费的API。正如您所提到的,它是每个应用程序许可的:(

  

或者简单地说,我怎样才能得到所有触点的协调点   Xamarin?

您应该为每个平台(或您的UWP)实现自定义渲染器。这就是格雷斯先生在幕后为你做的事情。如果您不想自己实现,目前唯一的选择是使用外部API。

也许有一天Xamarin会提供开箱即用的这种API ......让我们保持手指交叉!

希望这有帮助!

答案 1 :(得分:2)

最新,但是如果您使用Skia,则可以添加以下Nuget:
https://github.com/OndrejKunc/SkiaScene

您将能够跟踪场景内的触摸位置。