使用 Tapped GestureRecognizer 时如何获取标签的文本 属性 的值?

How to get value of the Text property of a Label tapped when using Tapped GestureRecognizer?

我在 CollectionView 中有一个 Label,我想将它作为参数传递给 Tapped 事件。我该怎么做?

我得到 Xamarin.Forms.Label 作为参数而不是 EmojiSource。

XAML

 <CollectionView
                    x:Name="collectionView"
                    Margin="-10,-15,-10,-10"
                    HeightRequest="270"
                    VerticalScrollBarVisibility="Never">

                    <CollectionView.ItemsLayout>
                        <GridItemsLayout Orientation="Horizontal" Span="5" />
                    </CollectionView.ItemsLayout>
                    <CollectionView.ItemTemplate>

                        <DataTemplate>

                            <Label
                                x:Name="Labellabel"
                                Margin="10"
                                FontSize="30"
                                Text="{Binding EmojiSource}"
                                TextColor="#FF000000">
                                <Label.GestureRecognizers>
                                    <!-- <TapGestureRecognizer Command="{Binding Path=BindingContext.EmojiTappedCommand, Source={x:Reference EditorView}}" CommandParameter="{Binding EmojiSource}" />-->
                                    <TapGestureRecognizer CommandParameter="{Binding EmojiSource}" Tapped="TapGestureRecognizer_Tapped" />
                                </Label.GestureRecognizers>

                            </Label>


                        </DataTemplate>
                    </CollectionView.ItemTemplate>

                </CollectionView>

代码隐藏

     ObservableCollection<Emojis> EmojiList = new ObservableCollection<Emojis>();
     collectionView.ItemsSource = EmojiList;

     public Control(){
    
      EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.SlightlySmilingFace) , EmojiMethodCommand = "smiley_face" });
      EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.FaceWithStuckOutTongueAndWinkingEye), EmojiMethodCommand = "upside_down_face" });
      EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.LoudlyCryingFace), EmojiMethodCommand = "fmiling_face_circle_eyes" });
      EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.WinkingFace), EmojiMethodCommand = "smiling_emoi_eyes" });
      EmojiList.Add(new Emojis { EmojiSource = Convert.ToString(Emoji.SmilingFaceWithHeartEyes), EmojiMethodCommand = "smiling_emoji_smiling_eyes" });
    }

    private void TapGestureRecognizer_Tapped(object sender, EventArgs e){
                var emoji = sender as string;
                EntryControl.Text = EntryControl.Text + emoji;
    }

根据你的代码我了解到

private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
{
    var emoji = sender as string;
    EntryControl.Text = EntryControl.Text + emoji;
}

使用发件人的文本 属性(标签),您应该没问题:

private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
{
    var emoji = ((Label)sender).Text;
    EntryControl.Text = EntryControl.Text + emoji;
}

还要注意你写的地方

<Label.GestureRecognizers>
    <!-- <TapGestureRecognizer Command="{Binding Path=BindingContext.EmojiTappedCommand, Source={x:Reference EditorView}}" CommandParameter="{Binding EmojiSource}" />-->
    <TapGestureRecognizer CommandParameter="{Binding EmojiSource}" Tapped="TapGestureRecognizer_Tapped" />
</Label.GestureRecognizers>

CommandParameter 无效,因为它适用于 Command 而不是 Tapped 事件。