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

标签 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)类 CustomImage :图像 { 公共(public)静态只读 BindableProperty CommandProperty = BindableProperty.Create(p => p.Command, null); 公共(public) ICommand 命令 { 获取 { 返回 (ICommand)GetValue(CommandProperty); } 设置 { SetValue(CommandProperty, 值); } }

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

...

最佳答案

如果您想在按下图像时放大图像,请尝试以下代码:

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"

关于c# - 如何扩展自定义解决方案 Xamarin 的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46339478/

相关文章:

c# - 如何在 xamarin 表单 webview 中加载本地 html 文件

c# - 比较使用 Thread.Sleep 和 Timer 延迟执行

c# - 可以(错误地)使用 Exception.HelpLink 来识别 Exception 对象吗?

c# - SonarQube 不会分析名为 Test* 的 C# 项目

c# - 显示来自 byte[ ] 的图像

c# - 无法连接到 Android 上的 TCP 端口

c# - 如何使用 C# 将 JSON 发布到服务器?

c# - Q : WPF - To what extent should Resource Dictionaries be avoided?

c# - WPF Datagrid - 订阅单元格加载事件?

c# - Xamarin Forms - Java.Interop.lz4 访问被拒绝错误