Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获取MotionEventActions.Up以获得呼叫?_C#_Android_Xamarin_Xamarin.forms_Custom Renderer - Fatal编程技术网

C# 如何获取MotionEventActions.Up以获得呼叫?

C# 如何获取MotionEventActions.Up以获得呼叫?,c#,android,xamarin,xamarin.forms,custom-renderer,C#,Android,Xamarin,Xamarin.forms,Custom Renderer,我已经编写了一个Android自定义渲染器来处理触摸事件。我能够成功处理MotionEventActions.Down事件,但渲染器中未调用其他事件 我正在尝试处理MotionEventActions.Down和MotionEventActions.Up 如何获取MotionEventActions.Up事件以调用 渲染器: using Android.Views; using Xamarin.Forms; using Xamarin.Forms.Platform.Androi

我已经编写了一个Android自定义渲染器来处理触摸事件。我能够成功处理MotionEventActions.Down事件,但渲染器中未调用其他事件

我正在尝试处理MotionEventActions.Down和MotionEventActions.Up

如何获取MotionEventActions.Up事件以调用

渲染器:

   using Android.Views;
   using Xamarin.Forms;
   using Xamarin.Forms.Platform.Android;
   using SecurityCompanyApp;
   using SecurityCompanyApp.Droid.Renderers;
   using System;

   #pragma warning disable CS0612 // Type or member is obsolete
   [assembly: ExportRenderer(typeof(TouchView), typeof(TouchViewRenderer))]
   #pragma warning restore CS0612 // Type or member is obsolete
   namespace SecurityCompanyApp.Droid.Renderers
   {
       [Obsolete]
       public class TouchViewRenderer : ViewRenderer
       {
           bool isTouchedDown = false;

           public override bool OnTouchEvent(MotionEvent e)
           {

               var touchView = Element as TouchView;

               switch (e.Action)
               {
                   case MotionEventActions.Down:
                       HandleTouchDown(touchView);
                       break;

                   case MotionEventActions.Up:
                       HandleTouchUp(touchView);
                       break;

                   default:
                       isTouchedDown = false;
                       break;
               }

               return base.OnTouchEvent(e);
           }

           private void HandleTouchUp(TouchView touchView)
           {
               isTouchedDown = false;
               touchView.TouchEnded();
           }

           private void HandleTouchDown(TouchView touchView)
           {
               if (!isTouchedDown)
               {
                   touchView.TouchStarted();
               }

               isTouchedDown = true;
           }

           public override bool OnInterceptTouchEvent(MotionEvent e)
           {
               BringToFront();
               return true;
           }
       }
   }
Xamarin.Forms.View:

using System;
using System.Threading;
using Xamarin.Forms;

namespace SecurityCompanyApp
{
    public partial class TouchView : ContentView
    {
        public event EventHandler TouchStart = delegate { };
        public event EventHandler TouchEnd = delegate { };

        public static readonly BindableProperty IsTouchingProperty = BindableProperty.Create(
          nameof(IsTouching),
          typeof(bool),
          typeof(TouchView),
          false);

        public bool IsTouching
        {
            get => (bool)GetValue(IsTouchingProperty);
            set => SetValue(IsTouchingProperty, value);
        }

        public static readonly BindableProperty TouchDurationProperty = BindableProperty.Create(
          nameof(TouchDuration),
          typeof(TimeSpan),
          typeof(TouchView),
          TimeSpan.Zero);

        public TimeSpan TouchDuration
        {
            get => (TimeSpan)GetValue(TouchDurationProperty);
            set => SetValue(TouchDurationProperty, value);
        }

        public static readonly BindableProperty MaxTouchDurationProperty = BindableProperty.Create(
          nameof(MaxTouchDuration),
          typeof(double),
          typeof(TouchView),
          10.0);

        public double MaxTouchDuration
        {
            get => (double)(GetValue(MaxTouchDurationProperty));
            set => SetValue(MaxTouchDurationProperty, value);
        }

        public static readonly BindableProperty TouchStartTimeProperty = BindableProperty.Create(
          nameof(TouchStartTime),
          typeof(DateTime),
          typeof(TouchView),
          new DateTime());

        public DateTime TouchStartTime
        {
            get => (DateTime)GetValue(TouchStartTimeProperty);
            set => SetValue(TouchStartTimeProperty, value);
        }

        public static readonly BindableProperty TouchEndTimeProperty = BindableProperty.Create(
          nameof(TouchEndTime),
          typeof(DateTime),
          typeof(TouchView),
          new DateTime());

        public DateTime TouchEndTime
        {
            get => (DateTime)GetValue(TouchEndTimeProperty);
            set => SetValue(TouchEndTimeProperty, value);
        }

        public void TouchStarted()
        {
            StartTouchTimer();
            TouchStart(this, default);
            IsTouching = true;
        }

        public void TouchEnded()
        {
            EndTouchTimer();
            IsTouching = false;
        }

        private bool isStopRequested;

        private void StartTouchTimer()
        {
            isStopRequested = false;

            TouchStartTime = DateTime.Now;

            Device.StartTimer(TimeSpan.FromSeconds(1/60f), ()=> 
            {
                if (isStopRequested)
                    return false;

                TouchDuration = DateTime.Now - TouchStartTime;

                if (TouchDuration >= TimeSpan.FromSeconds(MaxTouchDuration))
                    EndTouchTimer();

                return true;
            });

        }

        private void EndTouchTimer()
        {
            TouchEndTime = DateTime.Now;
            TouchDuration = TouchEndTime - TouchStartTime;
            isStopRequested = true;

            TouchEnd(this, default);
        }

    }
}
找到了答案:

OnTouchEvent
方法中,代替
返回base.OnTouchEvent(e)
强制方法返回
true