Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何更新ListCollectionView?_C#_Wpf - Fatal编程技术网

C# 如何更新ListCollectionView?

C# 如何更新ListCollectionView?,c#,wpf,C#,Wpf,上下文是我想从ViewModel的视图aListCollectionView中公开一个ListView;该ViewModel缓慢更新一个可观察集合(填充集合需要几秒钟) 因此,我想在可观察收集更新后更新列表收集视图: MyObservableCollection.CollectionChanged += CollectionChanged; private void CollectionChanged(object sender, NotifyCollectionChangedEventArg

上下文是我想从ViewModel的视图a
ListCollectionView
中公开一个
ListView
;该ViewModel缓慢更新一个
可观察集合
(填充集合需要几秒钟)

因此,我想在
可观察收集
更新后更新
列表收集视图

MyObservableCollection.CollectionChanged += CollectionChanged;

private void CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    MyListCollectionView.AddNewItem(e.NewItems);
    MyListCollectionView.CommitNew();
}
我试图更新
ListCollectionView
,但即使使用
列表
,也无法更新。我该怎么做

[TestClass]
public class ListViewTests
{
    private ListCollectionView _sut;

    [TestInitialize]
    public void Setup()
    {
        var toadd = new List<int> {};
        _sut = new ListCollectionView(toadd);
    }

    [TestMethod]
    public void AddItem()
    {
        var toadd = new List<int> { 1,2,3 };
        _sut.AddNewItem(toadd);
        _sut.CommitNew();
    }
}
在ViewModel中:

private object _o = new object();

Setup()
方法中的
toadd
类型更改为
List
或实现
IList
并实际提供
Add
方法实现的任何其他集合:

不能向数组中“添加”某些内容

也不清楚你的测试应该做什么?验证内置的
ListCollectionView
类型的功能?您应该专注于测试代码,而不是测试框架类型

*最后,我想在ObservableCollection更新后更新ListCollectionView

然后将
可观察集合
包装起来:

Parallel.ForEach(ShTiffFiles, file =>
{
    var sht = new ShutterTiff(file, _aesService); // slow (small Model)
    var shtv = new ShutterTiffVignette(sht, _fastCache); // slow (small ViewModel)
    lock (_o) // make it thread safe
    {
        Application.Current.Dispatcher.Invoke(() =>
        {
            ShutterTiffObservableCollection.Add(shtv);
        });
    }
});
_sut = new ListCollectionView(observableCollection);

或者直接修改
可观测采集
。您似乎不需要在此处使用
ListCollectionView
。集合视图是框架在绑定到源集合时为您创建的。有关更多信息,请参阅。

Setup()
方法中的
toadd
的类型更改为
列表
或实现
IList
并实际提供
添加
方法实现的任何其他集合:

不能向数组中“添加”某些内容

也不清楚你的测试应该做什么?验证内置的
ListCollectionView
类型的功能?您应该专注于测试代码,而不是测试框架类型

*最后,我想在ObservableCollection更新后更新ListCollectionView

然后将
可观察集合
包装起来:

Parallel.ForEach(ShTiffFiles, file =>
{
    var sht = new ShutterTiff(file, _aesService); // slow (small Model)
    var shtv = new ShutterTiffVignette(sht, _fastCache); // slow (small ViewModel)
    lock (_o) // make it thread safe
    {
        Application.Current.Dispatcher.Invoke(() =>
        {
            ShutterTiffObservableCollection.Add(shtv);
        });
    }
});
_sut = new ListCollectionView(observableCollection);

或者直接修改
可观测采集
。您似乎不需要在此处使用
ListCollectionView
。集合视图是框架在绑定到源集合时为您创建的。有关更多信息,请参阅。

我非常喜欢你的答案。从中学到了很多,也许她只是想在框架中为一些新的“东西”做一些游戏测试,而不是做一些实际的测试tests@styx:也许吧。这就是为什么我写了“unclark”。@Soleil MathieuPrévot:The
ListCollectionView
将数组包装在
Setup()
方法中。我编辑了我的答案来澄清这一点,因为你在测试方法中也添加了一个
。我真的很喜欢你的答案。从中学到了很多,也许她只是想在框架中为一些新的“东西”做一些游戏测试,而不是做一些实际的测试tests@styx:也许吧。这就是为什么我写了“unclark”。@Soleil MathieuPrévot:The
ListCollectionView
将数组包装在
Setup()
方法中。我编辑了我的答案以澄清这一点,因为在测试方法中也有一个
toadd
_sut.AddNewItem(toadd)int[]
更改为
List
,则code>将不起作用:以及为什么。。。为什么要使用
ListCollectionView
?相反,
可观察采集
直接采集。。。如果您真的需要使用LCV,为什么不使用
MyListCollectionView=new ListCollectionView(MyObservableCollection)
?那么您不需要安装集合更改事件handler@Selvin这是正确的观点。我不需要担心
ListCollectionView
,我只需要添加到
ObservableCollection
。那么,
AddNewItem
的作用是什么?@mm8你是在暗示有几种方法可以更新ListCollectionView吗?@mm8我确实错过了那句话。不过,在这种情况下,AddNewItem(Object)的作用是什么?除了mm8答案:
var toadd=newlist{1,2,3}_sut.AddNewItem(toadd)int[]
更改为
List
,则code>将不起作用:以及为什么。。。为什么要使用
ListCollectionView
?相反,
可观察采集
直接采集。。。如果您真的需要使用LCV,为什么不使用
MyListCollectionView=new ListCollectionView(MyObservableCollection)
?那么您不需要安装集合更改事件handler@Selvin这是正确的观点。我不需要担心
ListCollectionView
,我只需要添加到
ObservableCollection
。那么,
AddNewItem
的作用是什么?@mm8你是在暗示有几种方法可以更新ListCollectionView吗?@mm8我确实错过了那句话。不过,在这种情况下,AddNewItem(对象)的用途是什么?