[英]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.