如何检测列表视图xamarin表单中的可点击标签位置

How to detect clickable label position inside listview xamarin forms

我正在尝试做这样的事情

Like This

Listview 单击 label “编辑” 我希望当我单击此标签时检测到它的位置并显示

Aliaddress 的标签被点击

我为此使用了 TapGestureRecognizer,但是当我 google 时,我发现所选项目不适用于 TapGesture

这是我的xaml

<ListView ItemsSource="{Binding UserAdresses}" SelectedItem="{Binding SelectedAddress}" HorizontalOptions="{Binding HoriRLLR}" RowHeight="{Binding RowHeight}" VerticalOptions="FillAndExpand">
                            <ListView.ItemTemplate>
                                <DataTemplate>

                                    <ViewCell>
                                        <StackLayout VerticalOptions="FillAndExpand">
                                            <Label Text="{Binding Country}"  TextColor="Orange" FontSize="Large" FontAttributes="Bold"></Label>

                                            <Label Text="{Binding Address}"  TextColor="Black" FontSize="Medium"></Label>

                                            <Label Text="{Binding City}" TextColor="Black" FontSize="Medium"></Label>

                                            <Label Text="{translator:Translate Edit}" TextColor="Black" FontSize="Medium">

                                                <Label.GestureRecognizers>
                                                    <TapGestureRecognizer
                                                    Command="{Binding Path=BindingContext.EditAddressesCommand,  Source={x:Reference CustomerAdressesPage}}"/>
                                                </Label.GestureRecognizers>
                                                <Label.Effects>
                                                    <controls:UnderlineEffect></controls:UnderlineEffect>
                                                </Label.Effects>

                                            </Label>
                                        </StackLayout>
                                    </ViewCell>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>

我的代码

 public DelegateCommand EditAddressesCommand => new DelegateCommand(EditAddresses); 
        public DelegateCommand DeleteAddressesCommand => new DelegateCommand(DeleteAddresses);
        private readonly IPageDialogService _dialogService;

        private ObservableCollection<CustomerAdressesModel> _userAdresses;
        public ObservableCollection<CustomerAdressesModel> UserAdresses
        {
            get { return _userAdresses; }
            set { SetProperty(ref _userAdresses, value);
            }
        }


        private CustomerAdressesModel _selectedAddress;
        public CustomerAdressesModel SelectedAddress
        {
            get { return _selectedAddress; }
            set { SetProperty(ref _selectedAddress, value); }
        }


        private void EditAddresses()
        {
            _dialogService.DisplayAlertAsync("Test", "Edit Clicked", "Ok");
        }

我如何做到这一点并检测点击标签的位置

你可以使用这个:CommandParameter="{Binding .}" inside TapGestureRecognizer

Xaml:

 <Label.GestureRecognizers>
   <TapGestureRecognizer
           CommandParameter="{Binding .}"
           Command="{Binding Path=BindingContext.EditAddressesCommand,  Source={x:Reference CustomerAdressesPage}}"/>
 </Label.GestureRecognizers>

视图模型:

 public ICommand EditAddressesCommand
    {
        get
        {
            return new Command<YourModel>((YourModel model) =>
            {
                //Access your model properties
            });
        }
    }

希望这可以解决您的问题。