Xamarin.Forms-按钮按下&发行活动 [英] Xamarin.Forms - Button Pressed & Released Event

查看:103
本文介绍了Xamarin.Forms-按钮按下&发行活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在按下并释放按钮时触发我的事件,但是我只能在Xamarin.Forms中找到 Click 事件.

I want to my event to trigger when button pressed and released, but I can only find Click event in Xamarin.Forms.

我认为必须采取一些措施才能获得此功能.我的基本需求是按下按钮时启动一个过程,并在释放时停止.这似乎是一个非常基本的功能,但是Xamarin.Forms目前没有.

I believe there must be some work around to get this functionality. My basic need is to start a process when button is pressed and stop when released. It seems to be a very basic feature but Xamarin.Forms doesn't have it right now.

我在按钮上尝试了TapGestureRecognizer,但是按钮仅触发点击事件.

I tried TapGestureRecognizer on button, but button is firing only click event.

MyButton.Clicked += (sender, args) =>
{
  Log.V(TAG, "CLICKED");
};

var tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.Tapped += (s, e) => {
    Log.V(TAG, "TAPPED");
};
MyButton.GestureRecognizers.Add(tapGestureRecognizer);

请记住,我需要这些事件才能在Android和iOS上协同工作.

Keep in mind that I need those events to be working in Android and iOS togather.

推荐答案

最后,我得到了@Jason建议的解决方案.我们去...

Finally I got the solution suggested by @Jason. Here we go...

  1. 在PCL项目中使用事件创建Xamarin.Forms.Button的子类 处理能力

  1. Create sub class of Xamarin.Forms.Button in PCL project, with event handling capability

public class CustomButton : Button
{
    public event EventHandler Pressed;
    public event EventHandler Released;

    public virtual void OnPressed()
    {
      Pressed?.Invoke(this, EventArgs.Empty);
    }

    public virtual void OnReleased()
    {
      Released?.Invoke(this, EventArgs.Empty);
    }
}

  • 在相应项目中创建特定于平台的按钮渲染器

  • Create platform specific button renderer in respective project

    对于Andorid

    [assembly: ExportRenderer(typeof(Button), typeof(CustomButtonRenderer))]
    namespace WalkieTalkie.Droid.Renderer
    {
        public class CustomButtonRenderer : ButtonRenderer
        {
            protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
            {
                base.OnElementChanged(e);
    
                var customButton = e.NewElement as CustomButton;
    
                var thisButton = Control as Android.Widget.Button;
                thisButton.Touch += (object sender, TouchEventArgs args) =>
                {
                    if (args.Event.Action == MotionEventActions.Down)
                    {
                        customButton.OnPressed();
                    }
                    else if (args.Event.Action == MotionEventActions.Up)
                    {
                        customButton.OnReleased();
                    }
                };
            }
        }
    }
    

    对于IOS

    [assembly: ExportRenderer(typeof(CustomButton), typeof(CustomButtonRenderer))]
    namespace WalkieTalkie.iOS.Renderer
    {
        public class CustomButtonRenderer : ButtonRenderer
        {
            protected override void    OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
            {
                base.OnElementChanged(e);
    
                var customButton = e.NewElement as CustomButton;
    
                var thisButton = Control as UIButton;
                thisButton.TouchDown += delegate
                {
                    customButton.OnPressed();
                };
                thisButton.TouchUpInside += delegate
                {
                    customButton.OnReleased();
                };
            }
        }
    }
    

  • 在页面中实例化自定义按钮

  • Instantiate your custom button in your page

    var myButton = new CustomButton
    {
        Text = "CustomButton",
        HorizontalOptions = LayoutOptions.FillAndExpand
    };
    myButton.Pressed += (sender, args) =>
    {
        System.Diagnostics.Debug.WriteLine("Pressed");
    };
    myButton.Released += (sender, args) =>
    {
         System.Diagnostics.Debug.WriteLine("Pressed");
    };
    

  • 希望这对某人有所帮助:)

    Hope this help someone :)

    这篇关于Xamarin.Forms-按钮按下&amp;发行活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆