Xamarin 我是否可以使网格点击手势发送命令,然后使元素变为不可见?

Xamarin 我是否可以使网格点击手势发送命令,然后使元素变为不可见?,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="clr-namespac

我使用了以下模板:

<?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.Btn" x:Name="this">
    <StackLayout Padding="10"
                 HorizontalOptions="CenterAndExpand" 
                 VerticalOptions="CenterAndExpand" >
        <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="FontAwesome5ProRegular" />
        </Frame>
    </StackLayout>
</t:BaseButtonTemplate>

有没有一种使用绑定的方法,我可以这样做,当有一个点击的手势,命令将被发送,但也帧是不可见的。根据我有限的知识,我认为这可能是不可能的,但我想知道是否有人能想出一种方法来做到这一点。

好吧,简单地说,你能做的就是这样:

public event PropertyChangedEventHandler PropertyChanged;
protected virtual void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}

private bool _isvisible=true; //so that by default it is visible
public bool IsGridVisible { get {return _isvisible;} set {_isvisible=value; 
RaisePropertyChanged(nameof(IsGridVisible));}}
  • 继承已更改到类中的InotifyProperty

  • 实现其方法如下:

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void RaisePropertyChanged(string propertyName)
    {
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
    
    private bool _isvisible=true; //so that by default it is visible
    public bool IsGridVisible { get {return _isvisible;} set {_isvisible=value; 
    RaisePropertyChanged(nameof(IsGridVisible));}}
    
  • 然后将其绑定到要隐藏的任何控件:

  • 然后点击手势命令将此值更改为false

    void ontapgestureRecognitizerTapped(对象发送方、事件args args) { IsGridVisible=false; }

这应该足以让您的代码像一个魔咒一样工作


如有疑问,请回复

您可以绑定框架的IsVisible属性,并在点击命令时将其设置为false。这应该可以工作