如何在 Xamarin.Forms 中的运行时动态添加条目字段 [英] How to add entry fields dynamically during run time in Xamarin.Forms

查看:35
本文介绍了如何在 Xamarin.Forms 中的运行时动态添加条目字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Xamarin.Forms 开发一个项目,我需要在运行时动态创建输入字段.

I am developing a project using Xamarin.Forms, where I need to create entry fields dynamically during run-time.

例如,系统会询问用户在一个团队中参加比赛的球员人数,并根据用户提供的数据创建Entry 字段,例如姓名、年龄、联系人、等等.

For example, the user would be asked for the number of players playing in a team and based on the data provided by the user I need to create Entry fields such as name, age, contact, etc. for each player.

推荐答案

但这如何帮助我动态添加输入字段,即根据用户输入的玩家数量?

But how would this help me in adding the entry fields dynamically i.e. based on the number of players entered by the user?

首先,您可以通过 C# 动态创建它们,也可以添加它们并将它们隐藏在 Xaml 中.在 Xaml 方式的情况下,您可以做的是添加条目并将它们的可见性设置为 false,您实际上不需要创建 x 个条目,您只想根据玩家数量收集数据.
因此,首先,只需为您想要的每个字段添加一个条目:

First of all, you can either create them dynamically through C# or add them and hide them in Xaml. In case of Xaml way, what you can do is add the entries and set their visibily to false, you don't really need to create x numbers of entries, you just want to collect the data depending on the number of players.
So first of all, just add one entry for each field you want:

<Entry x:Name="NameEntry" IsVisible="false" />
<Entry x:Name="AgeEntry" IsVisible="false" />

现在,在用户提供他的玩家数量后,首先您将条目设为在您的 xaml.cs 中可见:

Now, after the user provides his number of players, first of all you turn the entries to be visible in your xaml.cs:

NameEntry.IsVisible = true;
AgeEntry.IsVisible = true;

您可以将玩家数量存储在一个变量中,我猜您会有一个按钮来保存数据,对吗?每次点击该按钮后,您检查保存的数据数量是否已达到玩家数量并清除条目,一旦达到您将它们恢复为不可见:

You can store your number of players in a variable, and I guess you will have a button to save data right? After each click on that button, you check if the number of saved data has reached the number of players and clear the entries, and once reached you turn them back to be invisible:

NameEntry.Text = string.Empty;
AgeEntry.Text = string.Empty;
numberOfSavedData++;
//your saving logic in your button click
if (numberOfSavedData == numberOfPlayers) 
{
    NameEntry.IsVisible = false;
    AgeEntry.IsVisible = false;
    //etc...
}

这并不是真正动态创建它们,这只是为了隐藏/显示条目.现在,如果您想创建它们然后稍后删除它们,您也可以在 xaml.cs 中进行.建议是在您的 xaml 中创建堆栈布局以向其中添加条目.

That's not really dynamically creating them, this is just to hide/show the entries. Now if you want to create them and then delete them later on, you can do it in your xaml.cs as well. Suggestion would be creating a stack layout in your xaml to add the entries to it.

<StackLayout x:Name="EntriesStackLayout">
</StackLayout>

在您的 xaml.cs 中,在提供玩家数量后,您只需添加 1 个所需数据条目,例如:

In your xaml.cs, after the number of players has been provided, you add just 1 entry of what data you need, like:

Entry nameEntry = new Entry();
Entry ageEntry = new Entry();
EntriesStackLayout.Children.Add(nameEntry);
EntriesStackLayout.Children.Add(ageEntry);

并且如前所述,您当然会有一个按钮来保存数据,并且您可以在处理程序中执行相同的操作,例如:

And as stated before, you would of course have a button to save data and you can do the same inside your handler like:

nameEntry.Text = string.Empty;
ageEntry.Text = string.Empty;
numberOfSavedData++;
//click handler
if (numberOfSavedData == numberOfPlayers)
{
    EntriesStackLayout.Children.Remove(nameEntry);
    EntriesStackLayout.Children.Remove(ageEntry);
    //etc...
}

这篇关于如何在 Xamarin.Forms 中的运行时动态添加条目字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆