I have this 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
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
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
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.
<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:
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.
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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.