我创建了示例应用程序来演示问题。抱歉,很难将所有代码放在这里,因为有模型类、数据模型、从 rest API 获取数据的服务文件。
因此,只包含少数提供信息的文件。
_placeList = await DataModel.PlaceDataSource.GetData(url);PlacePage.xaml.cs文件中的这一行语句实际上是在获取记录,但不会绑定并显示在listview中。
但是gridViewPlaces.ItemsSource = await DataModel.PlaceDataSource.GetData(url);
您可以在此处找到源代码。项目下载链接
MainPage.xaml
<SplitView x:Name="splitView" IsPaneOpen="True" OpenPaneLength="250" Grid.Row="1" DisplayMode="Inline">
<SplitView.Pane>
...
</SplitView.Pane>
<SplitView.Content>
<Grid>
<Frame x:Name="rootFrame" />
</Grid>
</SplitView.Content>
</SplitView>
PlacePage.xaml
<GridView Name="gridViewPlaces" ItemsSource="{x:Bind PlaceList}" SelectionMode="Single">
<GridView.ItemTemplate>
<DataTemplate>
<Grid Width="200" Height="Auto">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="Key" />
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Name}" />
<TextBlock Grid.Row="1" Grid.Column="0" Text="Value" />
<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Value}" />
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
PagePagePage.xaml.cs 文件
private IEnumerable<Place> _placeList;
public IEnumerable<Place> PlaceList
{
get { return _placeList; }
}
public event EventHandler GroupsLoaded;
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
url = e.Parameter.ToString();
LoadPlaces();
}
async private void LoadPlaces()
{
_placeList = await DataModel.PlaceDataSource.GetData(url);
//gridViewPlaces.ItemsSource = await DataModel.PlaceDataSource.GetData(url); // This works
gridViewPlaces.UpdateLayout();
if (GroupsLoaded != null)
GroupsLoaded(this, new EventArgs());
}
属性需要触发通知,让绑定知道发生了更改。照原样,当您替换_placeList时,您不会通知任何人 PlaceList 已更改,因此没有任何更新。此处的典型模式是只读初始化 PlaceList 属性,然后将内容添加到该现有集合,而不是交换该集合,但如果您通知已交换该集合,该集合也应该可以工作。
此外,PlaceList 中的 IEnumerable 需要在其内容更改时提供通知。执行此操作的标准方法是使其成为 ObservableCollection,因为 OC 为您实现了 INotifyPropertyChanged 和 INotifyCollectionChanged。请参阅绑定到集合快速入门