如何扩展自定义解决方案Xamarin的按钮

时间:2017-09-21 08:53:09

标签: c# xaml xamarin mvvm

您好如何在查看自定义解决方案的按钮中进行扩展?

我不会延伸这个:

...

 <Button BackgroundColor="#388fee" BorderColor="#388fee" BorderRadius="25" WidthRequest="50" HeightRequest="50"
                Command="{Binding CheckinShareCommand}" Margin="0,16">
            <Button.Image>
                <FileImageSource File="googlemap_view_share_button.png" />
            </Button.Image>
        </Button>

....

这是我的自定义解决方案。

按下图像时会放大图像

public class CustomImage:Image     {         public static readonly BindableProperty CommandProperty = BindableProperty.Create(p =&gt; p.Command,null);         公共ICommand命令         {             get {return(ICommand)GetValue(CommandProperty); }             set {SetValue(CommandProperty,value); }         }

    public static readonly BindableProperty CommandParameterProperty = BindableProperty.Create<CustomImage, object>(p => p.CommandParameter, null);
    public object CommandParameter
    {
        get { return (object)GetValue(CommandParameterProperty); }
        set { SetValue(CommandParameterProperty, value); }
    }

    private ICommand TransitionCommand
    {
        get
        {
            return new Command(async () =>
            {
                this.AnchorX = 0.48;
                this.AnchorY = 0.48;
                await this.ScaleTo(2.8, 50, Easing.Linear);
                await Task.Delay(100);
                await this.ScaleTo(1, 50, Easing.Linear);
                if (Command != null)
                {
                    Command.Execute(CommandParameter);
                }
            });
        }
    }

    public CustomImage()
    {
        Initialize();
    }


    public void Initialize()
    {
        GestureRecognizers.Add(new TapGestureRecognizer()
        {
            Command = TransitionCommand
        });

...

1 个答案:

答案 0 :(得分:1)

如果要在按下时放大图像,请尝试下面的代码:

public class CustomButton : Button
{
    public CustomButton() : base()
    {
        const int _animationTime = 10;
        Clicked += async (sender, e) =>
        {
            try
            {
                var btn = (CustomButton)sender;
                await btn.ScaleTo(1.2, _animationTime);
                await btn.ScaleTo(1, _animationTime);
            }
            catch (Exception ex)
            {
                ex.Track();
            }
        };

    }
}

<强>的Xaml

<userControls:CustomButton BackgroundColor="#388fee" BorderColor="#388fee" BorderRadius="25" WidthRequest="50" HeightRequest="50"
                Command="{Binding CheckinShareCommand}" Margin="0,16">
            <Button.Image>
                <FileImageSource File="googlemap_view_share_button.png" />
            </Button.Image>
        </userControls:CustomButton>

不要忘记将此行放入标题

xmlns:userControls="clr-namespace:YourNameSpace.UserControls"