Forms自定义按钮渲染器的Touch事件更改按钮图像

Forms自定义按钮渲染器的Touch事件更改按钮图像

本文介绍了如何使用Xamarin Forms自定义按钮渲染器的Touch事件更改按钮图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道了。
对于需要此服务的任何人。请参阅以下内容。观看了一百万次后,我就发现了

I figured it out.For anyone needing this. Please see the following.After Watching Xamarin Evolve a million times I caught on.

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

    }

}


推荐答案

这是一个如何在Xamarin表单中实现两个状态的ImageButton的示例:

Here is a sample how to implement a two state ImageButton in Xamarin Forms:

PCL:

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

Android渲染:

Android render:

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事件上升。

Note: in the Touch event set: e.Handled = false; to cause the Click event to rise.

这篇关于如何使用Xamarin Forms自定义按钮渲染器的Touch事件更改按钮图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 00:05