C# 为什么移动LongListSelector中的项会导致缺少项
我在LongListSelector中使用普通列表,ObservableCollection作为ItemSource 最初,所有精细滚动到列表末尾并返回到开头都没有问题 然后我调用ObservableCollection.Move(2,1)。现在,当我滚动到末尾并返回到顶部时(因此项目被重新实现),我得到了一个部分列表-列表开头的随机项目数不存在(例如:1,2,3…100变为1,3,42,43…100) 我是在用错误的方式做某事,还是它是LongListSelector中的一个bug 以下是我的问题的一个最小示例: TestPage.xaml.csC# 为什么移动LongListSelector中的项会导致缺少项,c#,windows-phone-8,observablecollection,updating,longlistselector,C#,Windows Phone 8,Observablecollection,Updating,Longlistselector,我在LongListSelector中使用普通列表,ObservableCollection作为ItemSource 最初,所有精细滚动到列表末尾并返回到开头都没有问题 然后我调用ObservableCollection.Move(2,1)。现在,当我滚动到末尾并返回到顶部时(因此项目被重新实现),我得到了一个部分列表-列表开头的随机项目数不存在(例如:1,2,3…100变为1,3,42,43…100) 我是在用错误的方式做某事,还是它是LongListSelector中的一个bug 以下是我的
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);
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
Net系统;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Navigation;
使用Microsoft.Phone.Controls;
使用Microsoft.Phone.Shell;
使用System.Collections.ObjectModel;
名称空间myApp.Pages
{
公共类项目
{
公共字符串文本{get;set;}
}
公共类ItemsViewModel
{
公共ObservableCollection UngroupedItems{get;set;}
}
公共部分类测试页:PhoneApplicationPage
{
ItemsViewModel\u vm;
公共测试页()
{
初始化组件();
_vm=新的ItemsViewModel();
_vm.UngroupedItems=新的ObservableCollection();
对于(int i=0;i<100;i++)
{
_添加(新项(){Text=string.Format(“-line{0}”,_vm.UngroupedItems.Count+1)});
}
数据上下文=_vm;
}
私有无效按钮\u单击(对象发送者,路由目标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>
绑定时,我从不喜欢这个函数 您可以看到,它甚至不会更新长列表选择器,直到您滚动回已更改的部分,这并不理想 在这一点上,我认为你必须自己安全地移动,除非其他人有更好的主意
private void Button_Click(object sender, RoutedEventArgs e)
{
SafeMove(2, 1);
}
private void SafeMove(int old_index, int new_index)
{
var saved_item = _vm.UngroupedItems[old_index];
_vm.UngroupedItems.RemoveAt(old_index);
_vm.UngroupedItems.Insert(new_index, saved_item);
}
模板版本
private void SafeMove<T>(ref ObservableCollection<T> collection, int old_index, int new_index)
{
var saved_item = collection[old_index];
collection.RemoveAt(old_index);
collection.Insert(new_index, saved_item);
private void SafeMove(参考ObservableCollection集合、int old_索引、int new_索引)
{
var saved_item=集合[旧索引];
收集.删除(旧索引);
集合。插入(新索引、已保存项);
}
使用安全移动,您将看到长列表选择器立即更新 谢谢你的方法;)是的,我已经被检查过了。但不确定这种方式是否总是不闪烁。然而,它看起来像是一个解决一些神奇问题的办法。我不喜欢魔术,我想知道这是一个LLS的问题,还是我误解了什么。你解决你的问题了吗?“我和你一样面临着问题。@shamimreza我最终用删除+插入来解决这个问题。但我离开了Windows平台,所以我不知道它的最新版本。