如何使用 tapGesture (Xamarin.Forms) 从应用程序中的多个标签中删除选定的标签

Posted

技术标签:

【中文标题】如何使用 tapGesture (Xamarin.Forms) 从应用程序中的多个标签中删除选定的标签【英文标题】:how to delete a selected label from multiple labels in an application using tapGesture (Xamarin.Forms) 【发布时间】:2016-07-22 10:42:51 【问题描述】:

我有一个按钮,每次点击时都会创建一个标签,假设我点击了 10 次,所以现在我创建了 10 个标签。如何删除我点击的随机标签?

PS: my labels are added to a stackLayout

我的应用程序的想法是创建一个待办事项列表。我有一个输入框和一个按钮。我在输入框中输入我想要执行的操作,然后单击按钮并创建一个标签,我刚刚输入该标签。当我完成某件事后,我想点击特定标签并将其删除,这怎么可能。有什么帮助吗?

代码:

var entry = new Entry();
entry.Placeholder = "type here";
entry.BackgroundColor = Color.White;
entry.PlaceholderColor = Color.Gray;
var newButton = new Button  Text = "+", BackgroundColor = Color.Purple, FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Button)), ;
StackLayout stackLayout = new StackLayout();
stackLayout.Children.Add(entry);
stackLayout.Children.Add(newButton);
this.Content = stackLayout;

var tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.Tapped += labelClick;
newButton.Clicked += (sender, args) =>  label = new Label();
label.BackgroundColor = Color.White;
label.TextColor = Color.Black;
label.Text = entry.Text;
entry.Text = "";
stackLayout.Children.Add(label);
label.GestureRecognizers.Add(tapGestureRecognizer);    

【问题讨论】:

拜托,你能添加一些你迄今为止尝试过的代码示例吗? var entry = new Entry(); entry.Placeholder = "在这里输入"; entry.BackgroundColor = 颜色.白色; entry.PlaceholderColor = 颜色.灰色; var newButton = new Button Text = "+", BackgroundColor = Color.Purple, FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Button)), ; StackLayout stackLayout = new StackLayout(); stackLayout.Children.Add(entry); stackLayout.Children.Add(newButton); this.Content = stackLayout; var tapGestureRecognizer = new TapGestureRecognizer(); tapGestureRecognizer.Tapped += labelClick; newButton.Clicked += (sender, args) => label = new Label(); label.BackgroundColor = 颜色.白色; label.TextColor = 颜色.黑色;标签.Text = entry.Text; entry.Text = ""; stackLayout.Children.Add(标签);标签.GestureRecognizers.Add(tapGestureRecognizer); ; 【参考方案1】:

嗯,我不知道你的代码是怎样的,这使得回答你的问题有点困难,但这里有一个想法:

如果您使用的是 MVVM 方法(我推荐),您可以使用此标签创建一个视图,将其绑定到具有待办事项数据(Id、名称、详细信息...)的 ViewModel,添加 TapGesture并在 ViewModel 中采取行动。

编辑:

不是最好的方法,也不是我会做的方法。但这是基于您的来源的解决方案:

    public MainPage()
    
        var entry = new Entry();
        entry.Placeholder = "type here";
        entry.BackgroundColor = Color.White;
        entry.PlaceholderColor = Color.Gray;
        var newButton = new Button  Text = "+", BackgroundColor = Color.Purple, FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Button)), ;
        StackLayout stackLayout = new StackLayout();
        stackLayout.Children.Add(entry);
        stackLayout.Children.Add(newButton);
        this.Content = stackLayout;

        newButton.Clicked += (sender, args) =>
        
            var label = new Label();
            label.BackgroundColor = Color.White;
            label.TextColor = Color.Black;
            label.Text = entry.Text;
            entry.Text = "";
            stackLayout.Children.Add(label);

            var tapGestureRecognizer = new TapGestureRecognizer();
            tapGestureRecognizer.Tapped += (sensder, e) => DeleteLabel(stackLayout, label);

            label.GestureRecognizers.Add(tapGestureRecognizer);
        ;
    

    void DeleteLabel(StackLayout stackLayout, Label label)
    
        stackLayout.Children.Remove(label);
    

【讨论】:

var entry = new Entry(); entry.Placeholder = "在这里输入"; entry.BackgroundColor = 颜色.白色; entry.PlaceholderColor = 颜色.灰色; var newButton = new Button Text = "+", BackgroundColor = Color.Purple, FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Button)), ; StackLayout stackLayout = new StackLayout(); stackLayout.Children.Add(frame1); stackLayout.Children.Add(entry); stackLayout.Children.Add(newButton); this.Content = stackLayout; var tapGestureRecognizer = new TapGestureRecognizer(); tapGestureRecognizer.Tapped += labelClick; newButton.Clicked += (sender, args) => label = new Label(); label.BackgroundColor = 颜色.白色; label.TextColor = 颜色.黑色;标签.Text = entry.Text; entry.Text = ""; stackLayout.Children.Add(标签);标签.GestureRecognizers.Add(tapGestureRecognizer); ; @Rémy 看看我提出的解决方案。

以上是关于如何使用 tapGesture (Xamarin.Forms) 从应用程序中的多个标签中删除选定的标签的主要内容,如果未能解决你的问题,请参考以下文章

我们如何使用 tapGesture 在 Tvos 中播放视频

如何在 UIView 中为 UIImageView 添加 tapGesture

如何使用iOS手势UIGestureRecognizer

SwiftUI TapGesture onStart / TouchDown

使用 tapGesture 自定义 UIView

TapGesture 在 Xcode 11.0 Beta 中不起作用