【问题标题】:Why moving an item in LongListSelector results to missing items为什么将 LongListSelector 中的项目移动到丢失项目
【发布时间】: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


【解决方案1】:

我从不喜欢绑定时的那个功能。

您可以看到它甚至不会更新您的长列表选择器,直到您滚动回已更改的部分,这并不理想。

在这一点上,我认为除非其他人有更好的主意,否则您必须自己安全地进行移动。

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);  

}


使用安全移动,您将看到长列表选择器立即更新。

【讨论】:

  • 谢谢你的方法;)是的,我已经检查过了。但不确定这种方式是否总是无闪烁。然而,它看起来像是一些神奇问题的解决方法。我不喜欢魔法,想知道这是 LLS 的问题还是我误解了什么。
猜你喜欢
  • 1970-01-01
  • 2013-05-06
  • 2012-12-22
  • 2018-07-02
  • 2021-12-15
  • 1970-01-01
  • 1970-01-01
  • 2020-07-11
  • 1970-01-01
相关资源
最近更新 更多