【发布时间】:2014-10-16 13:28:19
【问题描述】:
我在 LongListSelector 中使用普通列表,并将 ObservableCollection 作为 ItemSource。
最初一切正常 - 滚动到列表末尾并返回开头没有问题。
然后我调用 ObservableCollection.Move(2, 1)。现在,当我滚动到末尾并返回顶部时(因此项目被重新实现)我得到一个部分列表 - 列表开头的随机项目数不存在(例如:1,2,3 ...100 正在更改为 1,3,42,43..100)。
是我做错了什么,还是 LongListSelector 中的错误?
这是我的问题的一个最小示例:
TestPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Collections.ObjectModel;
namespace myApp.Pages
{
public class Item
{
public string Text { get; set; }
}
public class ItemsViewModel
{
public ObservableCollection<Item> UngroupedItems { get; set; }
}
public partial class TestPage : PhoneApplicationPage
{
ItemsViewModel _vm;
public TestPage()
{
InitializeComponent();
_vm = new ItemsViewModel();
_vm.UngroupedItems = new ObservableCollection<Item>();
for (int i = 0; i < 100; i++)
{
_vm.UngroupedItems.Add(new Item() { Text = string.Format("- line {0}", _vm.UngroupedItems.Count + 1) });
}
DataContext = _vm;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
_vm.UngroupedItems.Move(2, 1);
}
}
}
TestPage.xaml
<phone:PhoneApplicationPage
x:Class="myApp.Pages.TestPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d"
shell:SystemTray.IsVisible="True">
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<phone:LongListSelector ItemsSource="{Binding UngroupedItems}">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Text}" Style="{StaticResource PhoneTextTitle2Style}"/>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
<Button VerticalAlignment="Bottom" Content="Test" Click="Button_Click"></Button>
</Grid>
</phone:PhoneApplicationPage>
【问题讨论】:
-
你的问题解决了吗?我遇到了像你一样的问题。
-
@shamimreza 我最终使用了 remove+insert 来解决这个问题。但是我离开了 Windows 平台,所以我不知道它的最新版本。
标签: c# windows-phone-8 observablecollection updating longlistselector