【问题标题】:Bind Gesture Recognizers绑定手势识别器
【发布时间】:2023-03-04 21:13:01
【问题描述】:

我正在尝试在 Xml 中绑定手势识别器,以便在单击项目时进行处理

我尝试将它用于我的 XML

<DataTemplate x:Key="TextPostTemplate">
            <ViewCell>
                <StackLayout BackgroundColor="White" Margin="10, 10, 10, 10" Padding="10, 10, 10, 10">
                    <StackLayout Orientation="Horizontal">
                        <Label Text="{Binding Name}" TextColor = "Black" FontSize = "15"/>
                        <Image Source="options_icon.png" HeightRequest="15" HorizontalOptions="EndAndExpand" Margin="0, 0, 10, 0">
                            <Image.GestureRecognizers>
                                <TapGestureRecognizer Tapped="{Binding OptionClick}"/>
                            </Image.GestureRecognizers>
                        </Image>
                    </StackLayout>
                    <Label Text="{Binding Body}" TextColor = "Black"/>
                </StackLayout>
            </ViewCell>
        </DataTemplate>

我尝试将其绑定到

 foreach (var post in posts)
        {
      TapGestureRecognizer optionsClick = new TapGestureRecognizer();
            optionsClick.Tapped += (s, e) =>
            {
                ShowPostOptions(page, navigation, post.id, post.user);
            };
}

还有……

OptionClick = optionsClick

但我明白了

.xaml:错误:无法将“Xamarin.Forms.Xaml.ElementNode”类型的对象转换为“Xamarin.Forms.Xaml.ValueNode”类型。

【问题讨论】:

    标签: c# xml xaml xamarin xamarin.forms


    【解决方案1】:

    您的代码不起作用,因为您有效地将TapGestureRecognizer 的实例绑定到Tapped 事件。您不能对事件进行数据绑定,而且如果您要设置它,您可以将其设置为事件处理程序,而不是 TapGestureRecognizer 本身的实例。

    您有两个选择 - Event HandlerCommand

    事件处理程序

    在 XAML 中声明手势识别器如下:

    <TapGestureRecognizer Tapped="TappedHandler" />
    

    并在页面的代码隐藏中创建一个名为 TappedHandler 的事件处理程序:

    public void TappedHandler(object sender, EventArgs e)
    {
       ShowPostOptions(page, navigation, post.id, post.user);
    }
    

    命令

    在 XAML 中声明手势识别器如下:

    <TapGestureRecognizer Command="{Binding TapCommand}" />
    

    并在您的视图模型中创建一个命令来处理点击:

    private ICommand _tapCommand;
    
    public ICommand TapCommand => _tapCommand ?? 
         ( _tapCommand = new Command( 
              () => ShowPostOptions(page, navigation, post.id, post.user) ) );
    

    【讨论】:

      【解决方案2】:

      你正试图将一个事件绑定到一个命令,然后似乎又回来了,它只是不起作用

      <TapGestureRecognizer Tapped="{Binding OptionClick}"/>
      

      如果您的视图模型中有 ICommand,您可以这样做

      <TapGestureRecognizer Command="{Binding TapCommand}" ...
      

      如果你有一个事件准备好在后面的代码中进行,你可以这样做

      public void OnTapGestureRecognizerTappedEvet(Object sender,EventArgs e) 
      {
          // code here
      }
      
      <TapGestureRecognizer Tapped="OnTapGestureRecognizerTappedEvet" ...
      

      如果你想在代码中完成这一切,你可以这样做

      var tapGestureRecognizer = new TapGestureRecognizer();
      tapGestureRecognizer.Tapped += (s, e) => {
          // handle the tap
      };
      image.GestureRecognizers.Add(tapGestureRecognizer);
      

      Adding a Tap Gesture Gesture Recognizer

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-08
        • 1970-01-01
        • 1970-01-01
        • 2019-03-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多