[英]Adding elements to list of class in xamarin forms from user input
我有这个 class:
public class Player
{
public int PlayerID { get; set; }
public string PlayerName { get; set; }
public string PlayerTime { get; set; }
public int PlayerRank { get; set; }
}
此列表包含播放器 class
public List<Player> PlayersTable { get; set; }
PlayersTable = new List<Player>();
玩家 class 成员的值将来自不同的来源: PlayerName 将来自 xaml 中的用户输入“条目” - 将生成 PlayerID - PlayerTime 将来自秒表 - PlayerRank 将来自比较最佳时间
问题是:如何将来自不同来源的所有玩家数据添加到列表中? 我知道通过以下代码完成的硬编码:
PlayersTable = new List<Player>();
{
new Player()
{
PlayerID = 1,
PlayerName = "Yasir",
PlayerRank = 1,
PlayerTime = "11"
},
new Player()
{
PlayerID = 1,
PlayerName = "Ahmed",
PlayerRank = 2,
PlayerTime = "13"
}
但是如何从源中添加它(例如 PlayerName 将来自 xaml 条目的用户输入: <Entry Text="{Binding PlayerName}"/>
.. PlayerTime 将来自 Timer 的 (timer.Elapsed),谢谢
不确定您的代码中的 rest 是怎样的,但假设您想在 Entry 中输入玩家名称时添加一个新玩家,您需要添加一个命令以在用户按下返回键时运行。
<Entry Text="{Binding PlayerName}" ReturnCommand="{Binding CommandComplete}"></Entry>
然后在您的 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
}
首先,我建议您使用ObservableCollection<Player>
代替List<Player>
,因为ObservableCollection Class代表一个动态数据集合,它在添加、删除或刷新整个列表时提供通知。
然后我做一个简单的,你可以看看:
<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.