[英]Adding elements to list of class in xamarin forms from user input
I have this class:我有这个 class:
public class Player
{
public int PlayerID { get; set; }
public string PlayerName { get; set; }
public string PlayerTime { get; set; }
public int PlayerRank { get; set; }
}
And this list containing the player class此列表包含播放器 class
public List<Player> PlayersTable { get; set; }
PlayersTable = new List<Player>();
the values for player class members will come from different sources: PlayerName will come from user input "Entry" in xaml - PlayerID will be generated - PlayerTime will come from stopwatch - PlayerRank will come from comparing the best time玩家 class 成员的值将来自不同的来源: PlayerName 将来自 xaml 中的用户输入“条目” - 将生成 PlayerID - PlayerTime 将来自秒表 - PlayerRank 将来自比较最佳时间
the question is: how to add all Player data from different sources to the list?问题是:如何将来自不同来源的所有玩家数据添加到列表中? I know that hard coding done by the following code:
我知道通过以下代码完成的硬编码:
PlayersTable = new List<Player>();
{
new Player()
{
PlayerID = 1,
PlayerName = "Yasir",
PlayerRank = 1,
PlayerTime = "11"
},
new Player()
{
PlayerID = 1,
PlayerName = "Ahmed",
PlayerRank = 2,
PlayerTime = "13"
}
but how to add it from sources (like PlayerName will come from user input by xaml Entry: <Entry Text="{Binding PlayerName}"/>
.. And the PlayerTime will come from (timer.Elapsed) for Timer, thanks但是如何从源中添加它(例如 PlayerName 将来自 xaml 条目的用户输入:
<Entry Text="{Binding PlayerName}"/>
.. PlayerTime 将来自 Timer 的 (timer.Elapsed),谢谢
Not sure how the rest of your code looks like, but assuming you want to add a new player when you enter the player's name into Entry, you will need to add a command to run when the user presses the return key.不确定您的代码中的 rest 是怎样的,但假设您想在 Entry 中输入玩家名称时添加一个新玩家,您需要添加一个命令以在用户按下返回键时运行。
<Entry Text="{Binding PlayerName}" ReturnCommand="{Binding CommandComplete}"></Entry>
and then implement that command in your ViewModel where you have an instance of the list of players:然后在您的 ViewModel 中实现该命令,其中您有一个播放器列表的实例:
public partial class MyViewModel: INotifyPropertyChanged
{
public List<Player> PlayersTable { get; set; }
private int PlayerID { get; set; }
public string PlayerName { get; set; }
private string PlayerTime { get; set; }
private int PlayerRank { get; set; }
public System.Windows.Input.ICommand CommandComplete { get; set; }
public MyViewModel()
{
PlayersTable = new List<Player>();
CommandComplete = new Command(() =>
{
PlayerID = GeneratePlayerID(); // assuming you implemented something like this
PlayerRank = GetPlayerRank(); // assuming you implemented something like this
PlayerTime = GetPlayerTime(); // assuming you implemented something like this
Player player = new Player()
{
PlayerID = this.PlayerID,
PlayerName = this.PlayerName,
PlayerRank = this.PlayerRank,
PlayerTime = this.PlayerTime
};
PlayersTable.Add(player);
}
);
}
// the rest of the implementation of MyViewModel
}
Firstly, I suggest you to use ObservableCollection<Player>
to replace List<Player>
, because ObservableCollection Class represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed.首先,我建议您使用
ObservableCollection<Player>
代替List<Player>
,因为ObservableCollection Class代表一个动态数据集合,它在添加、删除或刷新整个列表时提供通知。
Then I do one simple that you can take a look:然后我做一个简单的,你可以看看:
<StackLayout>
<Label Text="PlayerName :" />
<Entry x:Name="entry1" />
<Button
x:Name="btnadd"
Command="{Binding addcommand}"
Text="add data" />
<ListView ItemsSource="{Binding PlayersTable}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal">
<Label Text="{Binding PlayerName}" />
<Label Text="{Binding PlayerTime}" />
<Label Text="{Binding PlayerRank}" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
public partial class Page25 : ContentPage
{
public ObservableCollection<Player> PlayersTable { get; set; }
public Command addcommand { get; }
public Page25()
{
InitializeComponent();
PlayersTable = new ObservableCollection<Player>()
{
new Player() {PlayerID = 1, PlayerName = "Yasir", PlayerRank = 1, PlayerTime = "11" },
new Player() {PlayerID = 1, PlayerName = "Ahmed", PlayerRank = 2, PlayerTime = "13" }
};
//add one test data to test.
addcommand = new Command(()=> {
Player player = new Player();
if(!string.IsNullOrEmpty(entry1.Text))
{
player.PlayerName = entry1.Text;
//get PlayerID by method.
player.PlayerID = 0;
//get PlayerTime by method or you can also get value from stopwatch.Don't know how do you use stopwatch.
player.PlayerTime = "test";
//get PlayerRank by method.
player.PlayerRank = 0;
PlayersTable.Add(player);
}
});
this.BindingContext = this;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.