【问题标题】:How to use Xamarin Forms Custom Button Renderer's Touch event to change the Buttons Image如何使用 Xamarin Forms 自定义按钮渲染器的触摸事件来更改按钮图像
【发布时间】:2016-05-16 13:46:10
【问题描述】:

我想通了。 对于任何需要这个的人。请参阅以下内容。 看了一百万次Xamarin Evolve 之后,我就明白了。

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");
            }
        };

    }

}

【问题讨论】:

  • 别忘了thisButton.Touch -= ... 否则你会有一些内存泄漏
  • 谢谢。我忘记了。
  • 我们应该什么时候做thisButton.Touch -= ...???
  • @JonathanMoss xamarin 按钮的 Click 事件在此实现后停止工作,仅适用于 android,不适用于 iOS。此外,android 点击事件按钮动画也不再起作用。你有什么解决办法吗?
  • 嗯,点击事件是Touch down,然后是Touch up。注册 Touch 事件时,它正在被处理,因此不会触发 click 事件。解决方法是设置 e.Handled = false;在 Touch 事件中。

标签: xaml xamarin custom-controls


【解决方案1】:

你需要打电话

Control.CallOnClick();

【讨论】:

    【解决方案2】:

    以下是如何在 Xamarin Forms 中实现两个状态的 ImageButton 的示例:

    PCL:

    public class FancyButton : Button
    {
        public void SendClickedCommand()
        {
            ICommand command = this.Command;
            if (command != null)
            {
                command.Execute(this.CommandParameter);
            }
        }
    }
    

    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);
        }
    }
    

    注意:在 Touch 事件集中:e.Handled = false; 会导致 Click 事件上升。

    【讨论】:

      猜你喜欢
      • 2018-07-23
      • 2018-03-15
      • 2016-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-09
      • 1970-01-01
      相关资源
      最近更新 更多