wpf - WPF 中数据绑定(bind) ObservableCollection 的序列化(PropertyChangedEventManager)

标签 wpf serialization

我试图通过数据绑定(bind)向 Listbox 显示一个列表。这是我的代码。

[Serializable]
public class RecordItem : INotifyPropertyChanged
{
    //implements of INotifyPropertyChanged
    public event PropertyChangedEventHandler PropertyChanged;
    protected void Notify(string propName) { if (this.PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propName)); } }
}


[Serializable]
public class Records : ObservableCollection<RecordItem>
{
    public UOCRecords() { }

    public void Serialize(string path)
    {
        BinaryFormatter binForm = new BinaryFormatter();
        using (FileStream sw = File.Create(path))
        {
            binForm.Serialize(sw, this);
            sw.Close();
        }
    }

    public static UOCRecords Deserialize(string path)
    {
        //...
    }
}

它基本上工作得很好,但是当我使用数据绑定(bind)时
this.lbData.ItemsSource = myRecents;

并尝试执行序列化
this.myRecents.Serialize(recentsPath);

它失败并出现此错误:

Type 'System.ComponentModel.PropertyChangedEventManager' in Assembly 'WpfApplication1, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not marked as serializable.



我该如何处理?

附言。我不想序列化 PropertyChangedEvent 处理程序。我想为此标记 [NonSerializable] 属性,但我不知道该怎么做。

最佳答案

I want to marking [NonSerializable] attribute to that, but I don't know how to that.



在这种情况下,您只需使用 [field:NonSerialized] 标记事件即可。属性:
[field:NonSerialized]
public event PropertyChangedEventHandler PropertyChanged;

关于wpf - WPF 中数据绑定(bind) ObservableCollection 的序列化(PropertyChangedEventManager),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7370687/

相关文章:

.net - 替换 WPF 入口点

java - 不是所有的对象都已经序列化了吗?

java客户端服务器端序列化问题

serialization - 带引用的 Xtext 序列化模型

c# - 绑定(bind)ContentControl内容以获取动态内容

javascript - 以编程方式以全屏模式打开 Youtube 的视频页面

c# - MVVM - 为 ModelView 实现 'IsDirty' 功能以保存数据

wpf - 在一页上打印 WPF 窗口

php - 在 PHP 中插入序列化数组

php - 在 php 中使用序列化将关联数组存储在数据库中