收集更改事件的深层复制Observable Collection

时间:2013-09-14 02:32:23

标签: c# observablecollection

这就是我现在所拥有的:

public ObservableCollection<ClientInfo> Clone(ObservableCollection<ClientInfo> clone)
{
    MemoryStream ms = new MemoryStream();
    BinaryFormatter bf = new BinaryFormatter();
    bf.Serialize(ms, clone);
    ms.Position = 0;
    object obj = bf.Deserialize(ms);
    ms.Close();
    return obj as ObservableCollection<ClientInfo>;
}

void ItemList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
    ObservableCollection<ClientInfo> list = sender as ObservableCollection<ClientInfo>;

    ItemList = Clone(list);
    //MessageBox.Show("Event: CollectionChanged");
}

在调试器中sender包含我的ObservableCollection,我可以看到所有的变量,它们似乎是正确的但是出于某种原因,手动将其转换为ObservableCollection似乎将其清空,因为我得到Argument Null Exception并且在调试器list确实为空。我做错了什么?

0 个答案:

没有答案