我正在做我的项目,我遇到了一个问题。我不知道如何显示来自
List
. 的数据
代码隐藏:
public ObservableCollection<GameResult> GameResultsToShow { get; set; }
= new ObservableCollection<GameResult>();
public void SortResults()
{
List<GameResult> SortedGameResults; //to bind
if (gameOption.gameType == GameType.Time)
SortedGameResults
= GameResults
.FindAll(
x => x.gameOption.gameLevel == gameOption.gameLevel
&& x.gameOption.gameType == gameOption.gameType)
.OrderBy(x => x.points)
.ToList();
else
SortedGameResults
= GameResults
.FindAll(
x => x.gameOption.gameLevel == gameOption.gameLevel
&& x.gameOption.gameType == gameOption.gameType)
.OrderBy(x => x.Time)
.ToList();
var GameResultsToShow = new ObservableCollection<GameResult>(SortedGameResults);
}
Xaml:
<CollectionView
ItemsSource="{Binding GameResultsToShow }"
BackgroundColor="PapayaWhip"
Margin="10"
Grid.Row="5"
Grid.ColumnSpan="3"
HorizontalOptions="Center">
<CollectionView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding GameResult}"/>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
ListView
可以使用TextCell
、ViewCell
等Cell控件,但是CollectionView
不能使用Cell控件。下面是示例代码供您参考:
Xaml:
<ListView
ItemsSource="{Binding GameResultsToShow }"
BackgroundColor="PapayaWhip">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding GameResultValue}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
代码隐藏:
public ObservableCollection<GameResult> GameResultsToShow { get; set; }
public MainPage()
{
InitializeComponent();
//Binding the itemsource as Jason suggested
GameResultsToShow = new ObservableCollection<GameResult>
{
new GameResult{GameResultValue = "Win"},
new GameResult{GameResultValue = "Lose"},
};
BindingContext = this;
}
型号:
public class GameResult
{
public string GameResultValue { get; set; }
}