以编程方式在 UWP ListView 中移动项目而不刷新 [英] programmatically move items in UWP ListView without refresh

查看:20
本文介绍了以编程方式在 UWP ListView 中移动项目而不刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的问题让我忙了一段时间,它看起来很基本,但它不起作用.归结为:

The following problem is keeping me busy now for quite some time, it seems to be so basic, yet it just doesn't work. It boils down to this:

  • 在后面的代码(ObservableCollection、ReactiveList 等)中有一个绑定到某个合适集合的 ListView
  • 我每 x 秒一次移动一个项目
  • 每次移动时,所有项目都会刷新(至少看起来是这样.一瞬间所有项目都消失了,然后以新的顺序重新出现)
  • Having a ListView that is bound to some suitable collection in code behind (ObservableCollection, ReactiveList, or alike)
  • I'm moving items one at a time every x seconds
  • on every move, all of the items get refreshed (at least it looks like that. for a split seconds all items disappear, then reappear in the new order)

必须有一种方法可以保留另一个元素并移动"移动的项目.我什至不关心移动项目的精美翻译动画,我只希望其他元素留在屏幕上.当然,我真正的用例不是随机移动项目,而是在后面的代码中对列表进行排序.但我将问题归结为这个简单的案例.

there must be a way to keep the other element and just "move" the moved item. I even don't care about a fancy translation animation for the moved item, I just want the other elements to stay on screen. of course my real usecase is not moving items randomly but sorting the list in code behind. But I tracked the issue down to this simple case.

有一个简单的 ListView 并绑定它,例如到 CodeBehind 中的 ObservableCollection,以下是我用于移动项目的虚拟代码:

having a simple ListView and having it bound e.g. to a ObservableCollection in CodeBehind, the following is my dummy code to move around the items:

_timer = new Timer(async _ =>
{
    Random r = new Random();
    var randomIndex = r.Next(0, contactsCvsSource.Count - 1);

    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { contactsCvsSource.Move(randomIndex, 5); });
}
, null, 0, 1000);

推荐答案

每次移动时,所有项目都会刷新(至少看起来是这样.一瞬间所有项目消失,然后以新顺序重新出现

on every move, all of the items get refreshed (at least it looks like that. for a split seconds all items disappear, then reappear in the new order

ObservableCollectionMove 方法用于提高UI 刷新性能.Move 方法调用时 UI 元素的哈希值不会改变,它只是刷新 ListView 而没有重新创建.

The Move method of ObservableCollection is used to improve UI refresh performance. The UI element hash value will not change when Move method invoke, it just refresh the ListView and did not recreate.

我只想让其他元素留在屏幕上.当然,我真正的用例不是随机移动项目,而是在后面的代码中对列表进行排序.

I just want the other elements to stay on screen. of course my real usecase is not moving items randomly but sorting the list in code behind.

根据您的需求,您可以通过InsertRemove 方法实现此功能.

For your requirement, you could realize this feature via Insert and Remove method.

var _timer = new Timer(async _ =>
 {
     Random r = new Random();
     var randomIndex = r.Next(0, Items.Count - 1);
     await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => {
         Items.Insert(5,Items[randomIndex] );
         Items.RemoveAt(randomIndex);                   
     });
 }
 , null, 0, 1000);      

这篇关于以编程方式在 UWP ListView 中移动项目而不刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆