Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何处理ListView的项';通过viewcell TapegestureRecognitor识别_Listview_Xamarin_Xamarin.forms - Fatal编程技术网

如何处理ListView的项';通过viewcell TapegestureRecognitor识别

如何处理ListView的项';通过viewcell TapegestureRecognitor识别,listview,xamarin,xamarin.forms,Listview,Xamarin,Xamarin.forms,我有一个带有自定义viewcell的ListView,我在其中放置了标签和手势识别器。 如何使用标签和TapGestureRecognitor处理不同的项目。这是我的密码 <ViewCell> <StackLayout> <StackLayout Orientation="Horizontal" Padding="5"> <Label Text="{Binding Title, Converter={StaticResource

我有一个带有自定义viewcell的ListView,我在其中放置了标签和手势识别器。 如何使用标签和TapGestureRecognitor处理不同的项目。这是我的密码

<ViewCell>
  <StackLayout>
    <StackLayout Orientation="Horizontal" Padding="5">
      <Label Text="{Binding Title, Converter={StaticResource LettersConverter}}" FontSize="30" TextColor="#edfff1"/>
      <Label Text="&#xf017;" FontSize="40">
        <Label.GestureRecognizers>
          <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" />
        </Label.GestureRecognizers>
      </Label>                                    
    </StackLayout>                                
    <StackLayout Orientation="Horizontal" Padding="1">
      <Label Text="Alarm" TextColor="#edfff1" FontSize="Micro"/>
      <Label Text="{Binding AlarmTime, StringFormat='{0:dddd, d MMMM, yyyy, HH:mm}'}"/>                                    
    </StackLayout>
  </StackLayout>
</ViewCell>
最终工作代码

Xaml:


如果我理解正确,您想知道单击了哪个标签。如果我是你,我会使用ICommand而不是事件处理程序和CommandParameter,它会为你提供类似于项目ID的东西

<TapGestureRecognizer Command="{Binding OnClickCommand}"
                      CommandParameter="{Binding ItemId}" />
Xamarin在他们的官方文档中有一个很好的教程,介绍如何做类似的事情


编辑:所以,这似乎就是您要寻找的

<TapGestureRecognizer Command="{Binding OnClickCommand}"
                      CommandParameter="{Binding .}" />

编辑2:这里的问题是TapGestureRecognitor没有指向页面的绑定上下文。您需要做的是:

<TapGestureRecognizer Command="{Binding Path = BindingContext.OnClickCommand, Source={x:Reference MainPage}}" CommandParameter="{Binding .}" />

另外,将x:Name添加到页面,如下所示:

ICommand _onClickCommand;

public ICommand OnClickCommand 
{
    get { return _onClickCommand; }
}

public YourViewModel() {
    OnClickCommand = new Command (OnClick);
}

void OnClick(object i) {
    var labelId = Convert.ToString(i);

    // Now you know which label was clicked and you can do something with it
}
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:App6"
             x:Class="TestApp.MainPage"
             x:Name="MainPage"> <!-- x:Reference points here -->

现在,当您点击标签时,OnClickCommand位于主BindingContext中,而不是ListView的ItemsSource中。

这非常简单。 3特别步骤

1将对象绑定到标签上,如CommandParameter=“{Binding.}”

2创建调用Tapped=“tappesturerecognizer\u Tapped”的函数

注意:您可以对任何对象使用此方法。 示例,如果您希望在listView中创建可单击的图像

var lbl = sender as Label; will be var img = sender as Image;
按钮将是var btn=发送者作为按钮

etc

这不正是我需要的。我需要将listView项作为已单击标签的父项。假设我在listView中有10个项目,每个项目都有一个带有TapGestureRecognitizer的标签,因此当用户单击标签时,应用程序需要知道列表中的哪个项目应该在数据库或listView中更改/更新。我的想法是,您可能已经有一个ID字段,因为数据库表也将有一个ID。但是,您可以用这个
“{Binding.}”
替换CommandParameter,以获得整个对象以及所需的所有属性。我更新了我的答案。谢谢。我现在无法检查此代码。明天我会让你知道的。RegardsIt不起作用。我也遵循了官方文件,但出于某种原因什么也没发生。如果我理解正确,“YourViewModel()”是代码隐藏中类的构造函数,对吗?@Klick是的,这就是构造函数。让我在一个小示例应用程序上快速尝试一下。
<TapGestureRecognizer Command="{Binding OnClickCommand}"
                      CommandParameter="{Binding .}" />
void OnClick(object i) {
    var model = (YourViewModel)i;

    // Now you can access model.AlarmTime, model.Title etc.
}
<TapGestureRecognizer Command="{Binding Path = BindingContext.OnClickCommand, Source={x:Reference MainPage}}" CommandParameter="{Binding .}" />
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:App6"
             x:Class="TestApp.MainPage"
             x:Name="MainPage"> <!-- x:Reference points here -->
<StackLayout Orientation="Horizontal" Padding="5">
  <Label Text="{Binding Title, Converter={StaticResource LettersConverter}}" FontSize="30" TextColor="#edfff1"/>
  <Label Text="&#xf017;" FontSize="40">
    <Label.GestureRecognizers>
      <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" CommandParameter="{Binding .}" />
    </Label.GestureRecognizers>
  </Label>                                    
</StackLayout>   
 private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
    {


        var lbl = sender as Label;

        var selectedItem = lbl.BindingContext as YourModel;
        if (selectedItem == null) return;
        // now you can access any property of your model
        // for example  var id = YourModel.id

        // or you can pass your model to a function like
        //HideorShowDetail(selectedItem);
    }
var lbl = sender as Label; will be var img = sender as Image;