如何在.NET Maui中获取触摸事件?

huangapple go评论116阅读模式
英文:

How can I get a touch event in .net Maui?

问题

我找不到一个适用于我的.NET MAUI应用程序的触摸事件处理程序。内置手势识别器具有TapGesture。对此进行的Google搜索没有返回任何结果。我向ChatGPT提问,它建议使用Microsoft.Maui.Essentials工具包中的Effects。然而,我甚至无法安装它,因为平台类型DotnetPlatform与我的项目不兼容,但查看文档似乎这是无用的LLVM建议。

我如何在.NET MAUI框架上监听TouchBegin和TouchEnd事件?

英文:

I can not find a touch event handler for my .net maui application. The built-in gesture recognizer has a TapGesture. Google searches for this returned nothing. I asked ChatGPT which recommended using Effects from the Microsoft.Maui.Essentials kit. However, I can not even installed it because the platform type DotnetPlatform is incompatible with my project, but looking at the Documentation it seems that this is useless LLVM advice.

How can I listen for a TouchBegin and TouchEnd event on a .net maui Frame?

答案1

得分: 1

在.NET MAUI中,如果您想监听特定的触摸事件,比如TouchBeginTouchEnd,您可能需要比内置的手势识别器如TapGesture更低级一些。

以下是实现这一目标的一般方法:

  1. 自定义渲染器方法:
    自定义渲染器方法允许您使用本机控件及其功能。通过为Frame编写自定义渲染器,您可以捕获本机触摸事件。

代码片段未经测试

对于Android:

using Android.Views;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.PlatformConfiguration.AndroidSpecific;
using YourNamespace;
using FrameRenderer = Microsoft.Maui.Controls.Compatibility.Platform.Android.FrameRenderer;

[assembly: ExportRenderer(typeof(Frame), typeof(CustomFrameRenderer))]
namespace YourNamespace
{
    public class CustomFrameRenderer : FrameRenderer
    {
        public CustomFrameRenderer(Context context) : base(context) { }

        public override bool OnTouchEvent(MotionEvent e)
        {
            if (e.Action == MotionEventActions.Down)
            {
                // TouchBegin
            }
            else if (e.Action == MotionEventActions.Up)
            {
                // TouchEnd
            }

            return base.OnTouchEvent(e);
        }
    }
}

对于iOS:

using UIKit;
using Microsoft.Maui.Controls;
using YourNamespace;
using FrameRenderer = Microsoft.Maui.Controls.Compatibility.Platform.iOS.FrameRenderer;

[assembly: ExportRenderer(typeof(Frame), typeof(CustomFrameRenderer))]
namespace YourNamespace
{
    public class CustomFrameRenderer : FrameRenderer
    {
        public override void TouchesBegan(NSSet touches, UIEvent evt)
        {
            base.TouchesBegan(touches, evt);
            // TouchBegin
        }

        public override void TouchesEnded(NSSet touches, UIEvent evt)
        {
            base.TouchesEnded(touches, evt);
            // TouchEnd
        }
    }
}
  1. 考虑使用SkiaSharp
    如果您的项目使用或计划使用SkiaSharp进行图形处理,它还提供了详细的触摸事件处理程序。

也许这不会直接帮助,但希望能为您提供正确的方向。希望对您有所帮助。

英文:

In .NET MAUI, if you want to listen for specific touch events like
TouchBegin and TouchEnd, you might need to go a bit lower-level than the built-in gesture recognizers like TapGesture.

Here's a general approach to achieving this:

  1. Custom Renderer Approach:
    The custom renderer approach allows you to use native controls and their features. By writing custom renderers for the Frame, you can capture native touch events.

Code snippets are untested

For Android:

using Android.Views;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.PlatformConfiguration.AndroidSpecific;
using YourNamespace;
using FrameRenderer = Microsoft.Maui.Controls.Compatibility.Platform.Android.FrameRenderer;

[assembly: ExportRenderer(typeof(Frame), typeof(CustomFrameRenderer))]
namespace YourNamespace
{
    public class CustomFrameRenderer : FrameRenderer
    {
        public CustomFrameRenderer(Context context) : base(context) { }

        public override bool OnTouchEvent(MotionEvent e)
        {
            if (e.Action == MotionEventActions.Down)
            {
                // TouchBegin
            }
            else if (e.Action == MotionEventActions.Up)
            {
                // TouchEnd
            }

            return base.OnTouchEvent(e);
        }
    }
}

For iOS:

using UIKit;
using Microsoft.Maui.Controls;
using YourNamespace;
using FrameRenderer = Microsoft.Maui.Controls.Compatibility.Platform.iOS.FrameRenderer;

[assembly: ExportRenderer(typeof(Frame), typeof(CustomFrameRenderer))]
namespace YourNamespace
{
    public class CustomFrameRenderer : FrameRenderer
    {
        public override void TouchesBegan(NSSet touches, UIEvent evt)
        {
            base.TouchesBegan(touches, evt);
            // TouchBegin
        }

        public override void TouchesEnded(NSSet touches, UIEvent evt)
        {
            base.TouchesEnded(touches, evt);
            // TouchEnd
        }
    }
}
  1. Consider Using SkiaSharp:
    If your project uses or plans to use SkiaSharp for graphics, it also provides detailed touch event handlers.

Perhaps it wont help directly but hopefully put you on the right track. I hope it helps.

huangapple
  • 本文由 发表于 2023年7月24日 18:04:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76753394.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定