我正在尝试使用ObservableCollection将数据绑定到listview。
我的XAML就像这样
<ScrollViewer x:Name="svListEarthquakes" Grid.Row="2" Grid.ColumnSpan="4" ViewChanged="svListEarthquakes_ViewChanged">
<ListView x:Name="listEarthquakes" IsItemClickEnabled="True" SelectionMode="Single" ItemsSource="{x:Bind ObsList}" ItemClick="listEarthquakes_ItemClick">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
<Setter Property="BorderBrush" Value="LightGray"></Setter>
<Setter Property="BorderThickness" Value="0,0,0,1"></Setter>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" TextWrapping="WrapWholeWords" Text="{Binding properties.mag}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<TextBlock Grid.Column="1" TextWrapping="WrapWholeWords" Text="{Binding Path=properties.time,Converter={StaticResource cvtDate}}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<TextBlock Grid.Column="2" TextWrapping="WrapWholeWords" Text="{Binding geometry.coordinates[0]}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<TextBlock Grid.Column="3" TextWrapping="WrapWholeWords" Text="{Binding geometry.coordinates[1]}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<TextBlock Grid.Column="4" TextWrapping="WrapWholeWords" Text="{Binding geometry.coordinates[2]}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<TextBlock Grid.Column="5" TextWrapping="WrapWholeWords" Text="{Binding properties.place}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ScrollViewer>
和C#代码
private async Task DisplayDataAsync()
{
Debug.WriteLine("BeginRequest " + DateTime.Now);
var repsonse = (Application.Current as App).gEarthquakesGeoJson = await HttpEarthquakes.GetdEarthquakesGeoJsonAsync<EarthquakesModel>(strRequestUrl);
if (repsonse.metadata.status == 200)
{
Debug.WriteLine("EndRequest " + DateTime.Now);
Debug.WriteLine("BeginxBind " + DateTime.Now);
Debug.WriteLine("Count " + repsonse.metadata.count);
ObsList.Clear();
if (repsonse.metadata.count > 0)
{
//List<FeaturesItem> fis = new List<FeaturesItem>();
foreach (FeaturesItem em in repsonse.features)
{
FeaturesItem fi = new FeaturesItem
{
type = em.type,
properties = em.properties,
geometry = em.geometry,
id = em.id
};
ObsList.Add(fi);
}
//listEarthquakes.ItemsSource = ObsList;
Debug.WriteLine("EndxBind " + DateTime.Now);
}
else
{
//No Data
}
}
else
{
//Net Error
}
}
注意: GetdEarthquakesGeoJsonAsync()返回所有数据。
例如:
BeginRequest 8/17/2017 5:38:20 PM
EndRequest 8/17/2017 5:38:21 PM
BeginxBind 8/17/2017 5:38:21 PM
数1798
EndxBind 8/17/2017 5:38:21 PM
我使用Debug.WriteLine打印时间,发现Web请求和响应时间非常短,而ObsList.Add(fi)也需要一些时间。
但是我的软件在向下滚动时非常慢。
所以如何改进它,非常感谢。
答案 0 :(得分:6)
ScrollViewer
以上的ListView
。它禁用ListView
虚拟化。 ListView
拥有ScrollViewer
。ListView
在此处阅读虚拟化:https://docs.microsoft.com/en-us/windows/uwp/debug-test-perf/optimize-gridview-and-listview