如何使用tapGesture(Xamarin.Forms)从应用程序中的多个标签中删除选定的标签
我有一个按钮,每次单击它时都会创建一个标签,假设我点击了它10次,所以现在我创建了10个标签。如何删除我点击的随机标签?如何使用tapGesture(Xamarin.Forms)从应用程序中的多个标签中删除选定的标签
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);
好了,我不知道你的代码是如何,这让人有点难以回答你的问题,但这里有一个想法:
如果您使用的是MVVM方法(我推荐),你可以用这个标签创建一个视图,用待处理的项目数据(Id,Name,Details,...)将它绑定到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 =“type here”; entry.BackgroundColor = Color.White; entry.PlaceholderColor = Color.Gray; VAR newButton =新的Button { 文本= “+”, BACKGROUNDCOLOR = Color.Purple, 字号= Device.GetNamedSize(NamedSize。小,typeof(Button)), }; –
StackLayout stackLayout = new StackLayout(); stackLayout.Children.Add(frame1); stackLayout.Children.Add(entry); stackLayout.Children.Add(newButton); this.Content = stackLayout; –
请问,你可以添加一些你已经尝试过的代码示例吗? – giacomelli
var entry = new Entry(); entry.Placeholder =“type here”; entry.BackgroundColor = Color.White; entry.PlaceholderColor = Color.Gray; VAR newButton =新按钮 { 文本= “+”, BACKGROUNDCOLOR = Color.Purple, 字号= Device.GetNamedSize(NamedSize.Small的typeof(按钮)), }; 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); }; –