【问题标题】:Serializing of Data-Bound ObservableCollection in WPF (PropertyChangedEventManager)WPF 中数据绑定 ObservableCollection 的序列化(PropertyChangedEventManager)
【发布时间】:2011-09-10 08:54:54
【问题描述】:

我尝试通过数据绑定向 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)
    {
        //...
    }
}

它基本上工作得很好,但是当我使用数据绑定时

this.lbData.ItemsSource = myRecents;

并尝试执行序列化

this.myRecents.Serialize(recentsPath);

失败并出现此错误:

程序集“WpfApplication1, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”中的类型“System.ComponentModel.PropertyChangedEventManager”未标记为可序列化。

我该如何处理?

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

【问题讨论】:

    标签: wpf serialization


    【解决方案1】:

    我想为此标记 [NonSerializable] 属性,但我没有 知道怎么做。

    在这种情况下,您只需使用[field:NonSerialized] 属性标记事件:

    [field:NonSerialized]
    public event PropertyChangedEventHandler PropertyChanged;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-06
      • 1970-01-01
      • 1970-01-01
      • 2011-06-16
      相关资源
      最近更新 更多