Xamarin 当我使用TapGestureRecognizer和带有消息的ViewModel发送命令时,如何添加按钮按下效果?
我使用此模板为我的应用程序创建可单击的按钮:Xamarin 当我使用TapGestureRecognizer和带有消息的ViewModel发送命令时,如何添加按钮按下效果?,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我使用此模板为我的应用程序创建可单击的按钮: <?xml version="1.0" encoding="UTF-8"?> <t:BaseButtonTemplate xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:t=
<?xml version="1.0" encoding="UTF-8"?>
<t:BaseButtonTemplate xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:t="clr-namespace:Japanese.Templates"
xmlns:local="clr-namespace:Japanese;assembly=Japanese"
x:Class="Japanese.Templates.Btn2" x:Name="this">
<StackLayout WidthRequest="50">
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Command="{Binding TapCommand, Source={x:Reference this}}"
CommandParameter="{Binding TapCommandParam, Source={x:Reference this}}"
NumberOfTapsRequired="1" />
</StackLayout.GestureRecognizers>
<Frame CornerRadius="25" BorderColor="{Binding FrameBorderColor, Source={x:Reference this}}"
BackgroundColor="{Binding FrameBackgroundColor, Source={x:Reference this}}"
HorizontalOptions="Center" VerticalOptions="Center" HasShadow="false" Padding="0"
WidthRequest="50" HeightRequest="50">
<Label TextColor="{Binding LabelTextColor, Source={x:Reference this}}"
Text="{Binding Text, Source={x:Reference this}}"
HorizontalOptions="Center" VerticalOptions="Center"
HorizontalTextAlignment="Center" VerticalTextAlignment="Center"
FontFamily="FontAwesome5ProSolid" />
</Frame>
</StackLayout>
</t:BaseButtonTemplate>
按钮的使用方式如下:
<t:Btn2 LabelTextColor="#EA4335"
Text="{Binding ABtnText }"
TapCommand="{Binding ABtnCmd }" Grid.Column="0" />
MessagingCenter.Subscribe<PhrasesFrameViewModel>(this, "ABtn", async (s) => await Btn((int)Settings.aBtn, 1));
在我的ViewModel中,我有以下代码:
public partial class PhrasesFrameViewModel : BaseViewModel
{
public ICommand ABtnCmd { get; }
public PhrasesFrameViewModel()
{
ABtnCmd = new Command(() => MessagingCenter.Send<PhrasesFrameViewModel>(this, "ABtn"));
}
public static readonly BindableProperty TapCommandProperty =
BindableProperty.Create(
"TapCommand", typeof(Command), typeof(BaseButtonTemplate),
defaultBindingMode: BindingMode.TwoWay,
defaultValue: default(Command));
public Command TapCommand
{
get { return (Command)GetValue(TapCommandProperty); }
set { SetValue(TapCommandProperty, value); }
}
public static readonly BindableProperty TapCommandParamProperty = BindableProperty.Create(nameof(TapCommandParam), typeof(object), typeof(BaseButtonTemplate), default(object));
public object TapCommandParam
{
get { return (object)GetValue(TapCommandParamProperty); }
set { SetValue(TapCommandParamProperty, value); }
}
public static readonly BindableProperty FrameBackgroundColorProperty =
BindableProperty.Create(
nameof(FrameBackgroundColor), typeof(Color), typeof(BaseButtonTemplate),
Color.FromHex("FFFFFF"));
public Color FrameBackgroundColor
{
get { return (Color)GetValue(FrameBackgroundColorProperty); }
set { SetValue(FrameBackgroundColorProperty, value); }
}
public static readonly BindableProperty FrameBorderColorProperty =
BindableProperty.Create(
nameof(FrameBorderColor), typeof(Color), typeof(BaseButtonTemplate),
Color.FromHex("EEEEEE"));
public Color FrameBorderColor
{
get { return (Color)GetValue(FrameBorderColorProperty); }
set { SetValue(FrameBorderColorProperty, value); }
}
公共部分类短语框架视图模型:BaseViewModel
{
公共ICommand ABtnCmd{get;}
公共短语框架视图模型()
{
ABtnCmd=new命令(()=>MessagingCenter.Send(这是“ABtn”);
}
公共静态只读BindableProperty TapCommandProperty=
BindableProperty.Create(
“TapCommand”、typeof(Command)、typeof(basebuttonemplate),
defaultBindingMode:BindingMode.TwoWay,
defaultValue:默认值(命令));
公共命令TapCommand
{
获取{return(Command)GetValue(TapCommandProperty);}
set{SetValue(TapCommandProperty,value);}
}
公共静态只读BindableProperty TapCommandParamProperty=BindableProperty.Create(nameof(TapCommandParam)、typeof(object)、typeof(BaseButtonTemplate)、default(object));
公共对象TapCommandParam
{
get{return(object)GetValue(TapCommandParamProperty);}
set{SetValue(TapCommandParamProperty,value);}
}
公共静态只读BindableProperty FrameBackgroundColorProperty=
BindableProperty.Create(
名称(FrameBackgroundColor)、类型(Color)、类型(BaseButtonTemplate),
颜色。FromHex(“FFFFFF”);
公共颜色框架背景颜色
{
获取{return(Color)GetValue(FrameBackgroundColorProperty);}
set{SetValue(FrameBackgroundColorProperty,value);}
}
公共静态只读BindableProperty FrameBorderColorProperty=
BindableProperty.Create(
nameof(FrameBorderColor)、typeof(Color)、typeof(BaseButtonTemplate),
颜色。来自十六进制(“EEEEEE”);
公共颜色边框颜色
{
获取{return(Color)GetValue(FrameBorderColorProperty);}
set{SetValue(FrameBorderColorProperty,value);}
}
我订阅的邮件如下:
<t:Btn2 LabelTextColor="#EA4335"
Text="{Binding ABtnText }"
TapCommand="{Binding ABtnCmd }" Grid.Column="0" />
MessagingCenter.Subscribe<PhrasesFrameViewModel>(this, "ABtn", async (s) => await Btn((int)Settings.aBtn, 1));
MessagingCenter.Subscribe(此“ABtn”,异步)=>wait Btn((int)Settings.ABtn,1));
我想做的是在单击按钮时创建某种效果
是否有人对我如何做到这一点有任何建议?请注意,效果可能是背景颜色更改或任何向用户指示按钮已按下的内容。如果您只想在xamarin表单中执行此操作(无本机渲染器)您必须在自定义按钮中订阅触摸事件。在这些事件中,您可以制作小规模动画或更改背景颜色。如果您想要一个行为类似的按钮,请使用它。您可以尝试创建自己的动画等,但我不确定是否值得。只需使用透明的常规按钮即可在你试图实现的任何其他布局之上。只是一个快速查询,为什么你不使用按钮而不是标签和框架不使用按钮,因为我想自定义它,我花了几天时间试图让文本与按钮控件对齐,并获得我想要的大小。