1
我想使用ObservableCollection將數據綁定到listview。UWP 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 2017年8月17日下午5點38分二十秒
EndRequest 2017年8月17日下午5點38分21秒
BeginxBind 2017年8月17日5 :38:21 PM
計數1798
EndxBind 2017年8月17日下午5點38分21秒
我使用Debug.WriteLine打印時間,發現web請求和響應時間很短,而ObsList.Add(fi),也需要一些時間。
但是我的軟件在滾動時非常慢。
那麼如何改進呢,非常感謝。
你不需要專門啓用ListView的虛擬化。它默認開啓。 –
哇,太棒了!非常感謝@Mikolaytis和@ Justin XL – Vincent