Xamarin.Forms - 按钮按下 &发布活动 [英] Xamarin.Forms - Button Pressed & Released Event
问题描述
我想在按钮按下和释放时触发我的事件,但我只能在 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...
在PCL项目中创建Xamarin.Forms.Button的子类,带事件处理能力
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
对于安卓
[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 - 按钮按下 &发布活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!