首页 > 解决方案 > How do I add more fields/entries when a button is selected using xamarin?

问题描述

I'm creating a mobile app using Xamarin (C# and XAML) for the first time. I would like to add functionality to add another Entry when the user selects the "Add" button.

More specifically, when the button is clicked I want it to create 2 new Entry fields. They are different from each other, but the same 2 appear on the page just above the button. If this isn't possible with XAML and C#, is there a way the button could unhide hidden Entries when clicked?

Thanks in advance.

标签: c#xamlxamarin

解决方案


“有没有一种方法可以在单击时取消隐藏隐藏的条目?”

是的,只需IsVisible在 .. 上设置属性即可Entry。简单示例:

<StackLayout Margin="0,50,0,0">
      <Entry x:Name="entry1" Placeholder="Entry 1..." IsVisible="false" />
      <Entry x:Name="entry2" Placeholder="Entry 2..." IsVisible="false" />
      <Button Text="Add entries" Clicked="Button_Clicked" />
</StackLayout>

后面的代码:

 void Button_Clicked(System.Object sender, System.EventArgs e)
    {
        entry1.IsVisible = true;
        entry2.IsVisible = true;
    }

推荐阅读