当我更新绑定到我的 ListView 的 ObservableCollection 时,它会自动滚动到顶部。
我获取数据的代码当前看起来像这样,记录是 ObsevableCollection:
public async Task getData()
{
var client = new HttpClient();
HttpResponseMessage response = await client.GetAsync(new Uri("https://api.nomics.com/v1/currencies/ticker?key=<api key>&limit=10"));
var jsonString = await response.Content.ReadAsStringAsync();
JsonArray root = JsonValue.Parse(jsonString).GetArray();
records.Clear();
for (uint i = 0; i < root.Count; i++)
{
string id = root.GetObjectAt(i).GetNamedString("id");
string name = root.GetObjectAt(i).GetNamedString("name");
decimal price = decimal.Parse(root.GetObjectAt(i).GetNamedString("price"));
records.Add(new Coin {
id = id,
name = name,
price = Math.Round(price, 4),
logo = "https://cryptoicon-api.vercel.app/api/icon/" + id.ToLower()
});
};
}
我的 XAML 布局:
<ListView x:Name="CoinsLV" Grid.Row="1" IsItemClickEnabled="True" ItemClick="listView_ItemClick" ScrollViewer.VerticalScrollMode="Auto">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Padding="5">
<Image Width="50" Height="50">
<Image.Source>
<BitmapImage UriSource="{Binding logo}" />
</Image.Source>
</Image>
<StackPanel>
<TextBlock Text="{Binding name}"
Margin="20,0,0,0"
FontSize="18"
FontWeight="SemiBold"
Foreground="DarkGray" />
<TextBlock Text="{Binding price}"
Margin="20,0,0,0"
FontSize="20"
Foreground="White"
Opacity="1" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsStackPanel ItemsUpdatingScrollMode="KeepItemsInView" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
有什么方法可以禁用此行为,因为它会导致非常糟糕的用户体验。我已经尝试单独更新每个项目,但无法使其正常工作。谢谢。