<StaticListExample>
<List Width="200">
<ListItem Text="Item 1" />
<ListItem Text="Item 2" />
<ListItem Text="Long Item 3" />
<ListItem Text="Item 4" />
</List>
</StaticListExample>
<ListItem Text="Item 4">
<CheckBox Alignment="Right">
</ListItem>
public class Highscore
{
public string Player;
public int Score;
}
public class DynamicListExample : UIView
{
public ObservableList<Highscore> Highscores;
public override void Initialize()
{
// initialize and populate highscores
Highscores = new ObservableList<Highscore>();
Highscores.Add(new Highscore { Player = "PlayerA", Score = 100 });
Highscores.Add(new Highscore { Player = "PlayerB", Score = 750 });
Highscores.Add(new Highscore { Player = "PlayerC", Score = 9000 });
Highscores.Add(new Highscore { Player = "PlayerD", Score = 9000 });
}
}
<DynamicListExample>
<List Items="{Highscores}" Width="200">
<ListItem IsTemplate="True" Text="{#Item.Player} {#Item.Score}" />
</List>
</DynamicListExample>
<List Items="{Highscores}">
<ListItem IsTemplate="True" .../>
<ListItem IsTemplate="True" Text="{#Item.Player} {#Item.Score}" />
<DynamicListExample>
<List Items="{Highscores}" Width="250">
<ListItem IsTemplate="True">
<Region Margin="10">
<Label Text="{#Item.Player}" Alignment="Left" FontColor="Black"
FontSize="22" AdjustToText="Width" />
<Label Text="{#Item.Score}" Alignment="Right" FontColor="Black"
AdjustToText="Width" />
</Region>
</ListItem>
</List>
</DynamicListExample>
public void AddAndRemove()
{
// remove item at index
Highscores.RemoveAt(0);
// remove item by reference
var item = Hishscores.Find(x => x.Player == "PlayerB");
Highscores.Remove(item);
// add new item
Highscores.Add(new Highscore { Player = "Player" , Score = 0 });
// add new item at index
Highscores.Insert(0, new Highscore { Player = "Player" , Score = 0 });
}
public void UpdateScores()
{
// scenario #1 - update the player's name on an item
var item = Highscores[0];
item.Player = "New Name";
Highscores.ItemModified(item, "Player");
// scenario #2 - add 100 to all scores
foreach (var highscore in Highscores)
{
highscore.Score += 100;
}
// ... update score on all items
Highscores.ItemsModified("Score");
// scenario #3 - update all fields on an item
var item2 = Highscores[1];
item2.Player = "New Name 2";
item2.Score = 0;
Highscores.ItemModified(item2);
// scenario #4 - update all fields on all list items
Highscores.ItemsModified();
// scenario #5 - update nested object fields on item
var item3 = Highscores[3];
item3.NestedObject.Field1 = "new value";
item3.NextedObject.Field2 = "new value";
// ... only Field1 on the nested object will be updated
Highscores.ItemModified(item3, "NestedObject.Field1");
// ... all fields on the nested object will be updated
Highscores.ItemModified(item3, "NestedObject");
}
<List Items="{Highscores}" ItemSelected="HighscoreSelected">
public void HighscoreSelected()
{
var selectedHighscore = Highscores.SelectedItem;
// do something with selected item
}
public int _nextIndex = 0;
public void SelectNext()
{
// you can set selected by index:
Highscores.SelectedIndex = _nextIndex;
// or set selected by reference:
//var item = Higscores[_nextIndex];
//Highscores.SelectedItem = item;
++_nextIndex;
if (_nextIndex >= Highscores.Count)
{
_nextIndex = 0;
}
}
public void SortHighscores()
{
// sort highscores by score (descending)
Highscores.Sort((x, y) => y.Score.CompareTo(x.Score));
// sort highscores by score (ascending)
//Highscores.Sort((x, y) => x.Score.CompareTo(y.Score));
// sort by using default comparer
//Highscores.Sort();
}
public void ReplaceHighscores()
{
// create new highscore list
var newScores = new List();
newScores.Add({ Player = "PlayerX", Score = 999 });
newScores.Add({ Player = "PlayerY", Score = 888 });
newScores.Add({ Player = "PlayerZ", Score = 777 });
// replace highscores with new list
Highscores.Replace(newScores);
// replace single item at index
var score = new Highscore { Player = "PlayerW", Score = 1000 };
Highscores.Replace(0, score);
}
Be notified when new themes, views, tutorials and updates are available