Как добавить эффект нажатия кнопки, когда я использую TapGestureRecognizer и ViewModel с Messaging для отправки команды? - PullRequest
0 голосов
/ 03 октября 2018

Я использую этот шаблон для создания кнопки, которую можно нажимать для моего приложения:

<?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" />

И в моей 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); }
    }

Я подписываюсь на следующее сообщение:

MessagingCenter.Subscribe<PhrasesFrameViewModel>(this, "ABtn", async (s) => await Btn((int)Settings.aBtn, 1));

Я хотел бы создать какой-то эффект при нажатии кнопки.

У кого-нибудь есть предложения, как мне это сделать?Обратите внимание, что эффект может быть изменением цвета фона или чем-то, что будет указывать пользователю, что кнопка была нажата.

1 Ответ

0 голосов
/ 03 октября 2018

Если вы хотите сделать это только в формах xamarin (без встроенных средств визуализации), вы должны подписаться в своей пользовательской кнопке на сенсорные события.В этих событиях вы можете сделать, например, небольшую анимацию или изменить цвет фона.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...