添加按钮效果后,Xamarin Forms按钮命令不触发

时间:2019-06-04 11:12:22

标签: xamarin xamarin.forms xamarin.android

我有一个像这样的按钮:

<Button Margin="0,20,0,0" Command="{Binding OnSkip}" BackgroundColor="{StaticResource Primary}" CornerRadius="2"
      Text="Terms and Conditions of Use" VerticalOptions="End" TextColor="White">
  <Button.Effects>
      <effects1:ButtonClickEffect></effects1:ButtonClickEffect>
  </Button.Effects>
</Button>

在按钮内部添加按钮效果后,“ OnSkip”命令不再触发,我不确定为什么。

按钮点击效果代码的实现如下:

public class AndroidButtonClickEffect : PlatformEffect
{
    protected override void OnAttached()
    {
        this.Control.Touch += this.Control_Touch;
    }

    private void Control_Touch(object sender, Android.Views.View.TouchEventArgs e)
    {
        if (e.Event.Action == MotionEventActions.Down)
        {
            this.SetColor(Android.Graphics.Color.Blue);
        }
        else if (e.Event.Action == MotionEventActions.Up)
        {
            this.SetColor(Android.Graphics.Color.LightBlue);
        }
    }

    private void SetColor(Android.Graphics.Color color)
    {
        this.Control.SetBackgroundColor(color);
    }

    protected override void OnDetached()
    {
        this.Control.Touch -= this.Control_Touch;
    }
}

删除按钮效果会使命令再次触发。为什么按钮效果会干扰命令触发?有没有一种方法可以使我获得效果来调用所需的命令(一般来说,我可以重用该效果)?

谢谢。

1 个答案:

答案 0 :(得分:1)

在主项目中,我添加了以下类,该类绑定了Command:

public class ButtonClickEffect : RoutingEffect
{
    public ButtonClickEffect() : base("Framework.ButtonClickEffect") { }

    public static readonly BindableProperty CommandProperty =
        BindableProperty.Create("Command", typeof(ICommand), typeof(ButtonClickEffect));

    public static ICommand GetCommand(BindableObject view)
    {
        return (ICommand)view.GetValue(CommandProperty);
    }

    public static void SetCommand(BindableObject view, ICommand value)
    {
        view.SetValue(CommandProperty, value);
    }

    public static readonly BindableProperty CommandParameterProperty =
        BindableProperty.CreateAttached("CommandParameter", typeof(object),
            typeof(ButtonClickEffect), (object)null);

    public static object GetCommandParameter(BindableObject view)
    {
        return view.GetValue(CommandParameterProperty);
    }

    public static void SetCommandParameter(BindableObject view, object value)
    {
        view.SetValue(CommandParameterProperty, value);
    }
}

Android实现的实现方式如下:

[assembly:ResolutionGroupName("Framework")]
[assembly:ExportEffect(typeof(AndroidButtonClickEffect), "ButtonClickEffect")]
namespace Framework.Droid.Effects
{
    public class AndroidButtonClickEffect : PlatformEffect
    {
        protected override void OnAttached()
        {
            Control.Touch += Control_Touch;
        }

        private void Control_Touch(object sender, Android.Views.View.TouchEventArgs e)
        {
            if (e.Event.Action == MotionEventActions.Down)
            {
                SetColor(Color.LightBlue);
            }
            else if (e.Event.Action == MotionEventActions.Up)
            {
                SetColor(Color.Blue);
            }

            var command = ButtonClickEffect.GetCommand(Element);
            command?.Execute(ButtonClickEffect.GetCommandParameter(Element));
        }

        private void SetColor(Color color)
        {
            Control.SetBackgroundColor(color);
        }

        protected override void OnDetached()
        {
            Control.Touch -= Control_Touch;
        }
    }
}

然后我从按钮中删除了'Command'属性,并按如下所示替换了它:

<Button Margin="0,20,0,0" BackgroundColor="{StaticResource Primary}" CornerRadius="2"
      Text="Terms and Conditions of Use" VerticalOptions="End" TextColor="White" 
      effects1:ButtonClickEffect.Command="{Binding OnSkip}" effects1:ButtonClickEffect.CommandParameter="{Binding .}">
  <Button.Effects>
      <effects1:ButtonClickEffect></effects1:ButtonClickEffect>
  </Button.Effects>
</Button>

老实说,命令绑定现在变得笨拙得多(所有这些代码只是为了获得简单的按钮效果),但是重要的是它现在可以工作了。现在,我需要确定如何实现iOS。

here来获得答案,以及将此URL发布为答案的人(该URL被删除)。