Xaml 如何使用Xamarin表单自定义按钮渲染器';s触摸事件以更改按钮图像
我想出来了。 给任何需要这个的人。请参阅以下内容。 看了一百万遍之后,我明白了Xaml 如何使用Xamarin表单自定义按钮渲染器';s触摸事件以更改按钮图像,xaml,xamarin,custom-controls,Xaml,Xamarin,Custom Controls,我想出来了。 给任何需要这个的人。请参阅以下内容。 看了一百万遍之后,我明白了 class LoginButtonCustomRenderer : ButtonRenderer { protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e) { base.OnElementChanged(e); Android.Wi
class LoginButtonCustomRenderer : ButtonRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
{
base.OnElementChanged(e);
Android.Widget.Button thisButton = Control as Android.Widget.Button;
thisButton.Touch += (object sender, TouchEventArgs e2) =>
{
if (e2.Event.Action == MotionEventActions.Down)
{
System.Diagnostics.Debug.WriteLine("TouchDownEvent");
// Had to use the e.NewElement
e.NewElement.Image = "pressed.png";
}
else if (e2.Event.Action == MotionEventActions.Up)
{
System.Diagnostics.Debug.WriteLine("TouchUpEvent");
}
};
}
}
类LoginButtonCustomRenderer:ButtonRenderer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
Android.Widget.Button thisButton=控件为Android.Widget.Button;
此按钮。Touch+=(对象发送者,TouchEventArgs e2)=>
{
if(e2.Event.Action==MotionEventActions.Down)
{
System.Diagnostics.Debug.WriteLine(“触地事件”);
//必须使用e.NewElement
e、 NewElement.Image=“pressed.png”;
}
else if(e2.Event.Action==MotionEventActions.Up)
{
System.Diagnostics.Debug.WriteLine(“TouchUpEvent”);
}
};
}
}
你需要打电话
Control.CallOnClick() 你需要打电话吗
Control.CallOnClick() 以下是如何在Xamarin表单中实现双状态ImageButton的示例: PCL: Android渲染:
public class FancyButtonAndroid : ButtonRenderer
{
Android.Widget.Button thisButton;
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
{
base.OnElementChanged(e);
thisButton = Control as Android.Widget.Button;
thisButton.SetBackgroundResource(Resource.Drawable.btn_unpress);
thisButton.Touch += ThisButton_Touch;
thisButton.Click += HandleButtonClicked;
}
private void ThisButton_Touch(object sender, TouchEventArgs e)
{
e.Handled = false;
if (e.Event.Action == MotionEventActions.Down)
{
System.Diagnostics.Debug.WriteLine("TouchDownEvent");
thisButton.SetBackgroundResource(Resource.Drawable.btn_press);
}
else if (e.Event.Action == MotionEventActions.Up)
{
System.Diagnostics.Debug.WriteLine("TouchUpEvent");
thisButton.SetBackgroundResource(Resource.Drawable.btn_unpress);
}
}
private void HandleButtonClicked(object sender, EventArgs e)
{
if (Element != null && Element is FancyButton)
{
(Element as FancyButton).SendClickedCommand();
}
}
protected override void Dispose(bool disposing)
{
if (thisButton != null)
{
thisButton.Touch -= ThisButton_Touch;
thisButton.Click -= HandleButtonClicked;
}
base.Dispose(disposing);
}
}
公共类FancyButtonAndroid:ButtonRenderer
{
Android.Widget.Button此按钮;
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
thisButton=控件为Android.Widget.Button;
此按钮.SetBackgroundResource(Resource.Drawable.btn_unpress);
thisButton.Touch+=thisButton\u Touch;
此按钮。单击+=把手按钮单击;
}
私有无效此按钮\u触摸(对象发送器,触摸事件参数e)
{
e、 已处理=错误;
if(e.Event.Action==MotionEventActions.Down)
{
System.Diagnostics.Debug.WriteLine(“触地事件”);
此按钮。SetBackgroundResource(Resource.Drawable.btn_press);
}
else if(e.Event.Action==MotionEventActions.Up)
{
System.Diagnostics.Debug.WriteLine(“TouchUpEvent”);
此按钮.SetBackgroundResource(Resource.Drawable.btn_unpress);
}
}
private void HandleButtonClicked(对象发送方,事件参数e)
{
if(Element!=null&&Element为FancyButton)
{
(元素为FancyButton).SendClickedCommand();
}
}
受保护的覆盖无效处置(布尔处置)
{
如果(此按钮!=null)
{
thisButton.Touch-=thisButton\u Touch;
此按钮。单击-=把手按钮单击;
}
基地。处置(处置);
}
}
注意:在触摸事件集中:
e.Handled=false
导致单击事件上升。以下是如何在Xamarin表单中实现双状态ImageButton的示例:
PCL:
Android渲染:
public class FancyButtonAndroid : ButtonRenderer
{
Android.Widget.Button thisButton;
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
{
base.OnElementChanged(e);
thisButton = Control as Android.Widget.Button;
thisButton.SetBackgroundResource(Resource.Drawable.btn_unpress);
thisButton.Touch += ThisButton_Touch;
thisButton.Click += HandleButtonClicked;
}
private void ThisButton_Touch(object sender, TouchEventArgs e)
{
e.Handled = false;
if (e.Event.Action == MotionEventActions.Down)
{
System.Diagnostics.Debug.WriteLine("TouchDownEvent");
thisButton.SetBackgroundResource(Resource.Drawable.btn_press);
}
else if (e.Event.Action == MotionEventActions.Up)
{
System.Diagnostics.Debug.WriteLine("TouchUpEvent");
thisButton.SetBackgroundResource(Resource.Drawable.btn_unpress);
}
}
private void HandleButtonClicked(object sender, EventArgs e)
{
if (Element != null && Element is FancyButton)
{
(Element as FancyButton).SendClickedCommand();
}
}
protected override void Dispose(bool disposing)
{
if (thisButton != null)
{
thisButton.Touch -= ThisButton_Touch;
thisButton.Click -= HandleButtonClicked;
}
base.Dispose(disposing);
}
}
公共类FancyButtonAndroid:ButtonRenderer
{
Android.Widget.Button此按钮;
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
thisButton=控件为Android.Widget.Button;
此按钮.SetBackgroundResource(Resource.Drawable.btn_unpress);
thisButton.Touch+=thisButton\u Touch;
此按钮。单击+=把手按钮单击;
}
私有无效此按钮\u触摸(对象发送器,触摸事件参数e)
{
e、 已处理=错误;
if(e.Event.Action==MotionEventActions.Down)
{
System.Diagnostics.Debug.WriteLine(“触地事件”);
此按钮。SetBackgroundResource(Resource.Drawable.btn_press);
}
else if(e.Event.Action==MotionEventActions.Up)
{
System.Diagnostics.Debug.WriteLine(“TouchUpEvent”);
此按钮.SetBackgroundResource(Resource.Drawable.btn_unpress);
}
}
private void HandleButtonClicked(对象发送方,事件参数e)
{
if(Element!=null&&Element为FancyButton)
{
(元素为FancyButton).SendClickedCommand();
}
}
受保护的覆盖无效处置(布尔处置)
{
如果(此按钮!=null)
{
thisButton.Touch-=thisButton\u Touch;
此按钮。单击-=把手按钮单击;
}
基地。处置(处置);
}
}
注意:在触摸事件集中:
e.Handled=false
导致单击事件上升。不要忘记此按钮。触摸-=…
否则您会有一些内存泄漏,谢谢。我忘了。我们应该什么时候做这个按钮。触摸-=…?@JonathanMoss xamarin按钮的点击事件在此实现后停止工作,仅适用于android,不适用于iOS。此外,android点击事件按钮动画也不再有效。你们有什么解决办法吗?嗯,点击事件是触地,然后是触地。注册触摸事件时,它正在被处理,因此不会触发单击事件。解决方案是将e.Handled设置为false;在Touch事件中。不要忘记此按钮。Touch-=…
否则您会有一些内存泄漏,谢谢。我忘了。我们应该什么时候做这个按钮。触摸-=…?@JonathanMoss xamarin按钮的点击事件在此实现后停止工作,仅适用于android,不适用于iOS。此外,android点击事件按钮动画也不再有效。你们有什么解决办法吗?嗯,点击事件是触地,然后是触地。注册触摸事件时,它正在被处理,因此不会触发单击事件。解决方案是将e.Handled设置为false;在触摸事件中。