当使用隐藏代码触发时,Xamarin.forms将变量传递给点击手势

当使用隐藏代码触发时,Xamarin.forms将变量传递给点击手势,xamarin.forms,uitapgesturerecognizer,Xamarin.forms,Uitapgesturerecognizer,我有一个for循环,它将循环出xaml.cs中的每个视图单元格。我向标签添加了一个点击手势,但我需要通过传递过滤器名称来区分单击的不同标签 下面是我在TableView中循环单元格的代码 for (int m = 0; m < filterList.Count; m++) { ViewCell vc = new ViewCell { View = new StackLayout {

我有一个for循环,它将循环出xaml.cs中的每个视图单元格。我向标签添加了一个点击手势,但我需要通过传递过滤器名称来区分单击的不同标签

下面是我在TableView中循环单元格的代码

for (int m = 0; m < filterList.Count; m++) {
                ViewCell vc = new ViewCell {
                    View = new StackLayout {
                        Orientation = StackOrientation.Horizontal,
                        HorizontalOptions = LayoutOptions.Start,
                        Children = {
                            new Label () {
                                Text = "   " + filterList[m].filterName,
                                YAlign = TextAlignment.Center,
                                GestureRecognizers = {
                                    new TapGestureRecognizer() {
                                        Command = new Command(TapL_Tapped),
                                        CommandParameter = filterList[m].filterName
                                    }
                                }
                            },
                            new Image () {
                                Source = FileImageSource.FromFile("check_mark.jpg"),
                                HorizontalOptions = LayoutOptions.EndAndExpand
                            }
                        }
                    }
                };
                tableView.Root [0].Add (vc);

引用命令时,执行函数通常接受对象参数

应该可以工作,但我目前没有一个快速的项目要测试。CommandParameter应该被传递

看看这一点,您将了解命令是如何工作的


MVVMLight等框架使用名为RelayCommand的类,它只实现ICommand。

我认为这不是最好的解决方案,但可以解决这个问题

在“tableView.Root[0].Add(vc);”的下面,您可以放置以下行:

tableView.Root [0].Last().GestureRecognizers.Add(new TapGestureRecognizer() {
                                          Command = new Command(TapL_Tapped),
                                          CommandParameter =   filterList[m].filterName
});
别忘了删除已经存在的“新TapGestureRecognitor”

我希望我帮了忙。 拥抱

void TapL_Tapped(object parameter)
tableView.Root [0].Last().GestureRecognizers.Add(new TapGestureRecognizer() {
                                          Command = new Command(TapL_Tapped),
                                          CommandParameter =   filterList[m].filterName
});