woj*_*tek 0 list observablecollection maui
我正在做我的项目,遇到了一个问题。我不知道如何显示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);
}
Run Code Online (Sandbox Code Playgroud)
XML:
<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>
Run Code Online (Sandbox Code Playgroud)
ListView可以使用TextCell、ViewCell等单元格控件。但是,CollectionView不能使用单元格控件。下面是示例代码供您参考:
XML:
\n<ListView \n ItemsSource="{Binding GameResultsToShow }"\n BackgroundColor="PapayaWhip">\n \n <ListView.ItemTemplate>\n <DataTemplate>\n <TextCell Text="{Binding GameResultValue}"/>\n </DataTemplate>\n </ListView.ItemTemplate>\n</ListView>\nRun Code Online (Sandbox Code Playgroud)\n隐藏代码:
\npublic ObservableCollection<GameResult> GameResultsToShow { get; set; } \n\npublic MainPage()\n{\n\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82InitializeComponent();\n\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82\n\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82//Binding the itemsource as Jason suggested\n\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82GameResultsToShow = new ObservableCollection<GameResult>\n\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82{\n\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82new GameResult{GameResultValue = "Win"},\n\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82new GameResult{GameResultValue = "Lose"},\n\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82\xe2\x80\x82};\n\n BindingContext = this;\n}\nRun Code Online (Sandbox Code Playgroud)\n模型:
\npublic class GameResult \n{\n public string GameResultValue { get; set; }\n}\nRun Code Online (Sandbox Code Playgroud)\n