本文介绍了为什么我的ObservableCollection序列化不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对该ObservableCollection进行序列化和反序列化:

I'm trying to serialize and deserialize this ObservableCollection:

public class DataCollection : ObservableCollection<Data>
{
}

public class Data : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private bool? _enabled;

    public string Name { get; set; }
    public bool? Enabled
    {
        get { return _enabled; }
        set { _enabled = value; NotifyPropertyChanged("Enabled"); }
    }

    public Data(string name, bool? enabled)
    {
        this.ScriptName = name;
        this.Enabled = enabled;
    }

    private void NotifyPropertyChanged(string property)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(property));
    }
}

使用此类(如示例):

class UserPreferences
{
    private DataCollection _dataLst;
    private const string _dataLstFileName = "Data.xml";

    public DataCollection DataLst { get { return _dataLst; } set { _dataLst = value; } }

    public UserPreferences()
    {
        Load();
    }

    public void Load()
    {
        if (File.Exists(_dataLstFileName))
        {
            using (var reader = new StreamReader(_dataLstFileName))
            {
                var xs = new XmlSerializer(typeof(DataCollection));
                _dataLst = (DataCollection) xs.Deserialize(reader);
            }
        }
        else
        {
            _dataLst = new DataCollection();
        }
    }

    public void Save()
    {
        using (var writer = new StreamWriter(_dataLstFileName))
        {
            var xs = new XmlSerializer(typeof(DataCollection));
            xs.Serialize(writer, _dataLst);
        }
    }
}

这就是我所说的它可以从我的应用中获取:

And here is how I call it from my app:

public partial class MainWindow : Window
{
    private DataCollection DataLst;

    ...

    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        var userPrefs = new UserPreferences();

        userPrefs.DataLst = DataLst; // DataList isn't null and contains correct data
        userPrefs.Save();
    }
}

但是它会创建空文件并挂断(即使没有例外,只是应用程序窗口变黑且没有响应)

But it creates empty file and hangs up (even without exceptions, just app window becomes black and doesn't response) in the line

var xs = new XmlSerializer(typeof(DataCollection));


推荐答案


  1. 您的数据类必须拥有较少参数的构造函数,否则XmlSerializer将永远无法创建Data实例。

  2. 应该存储和检索Data [],而不是存储DataCoollection,这比做任何事情都容易

  3. 存储时,可以调用ToArray方法获取Data []并使用typeof(Data [])进行序列化。

  4. 读取时您可以读取数组并将项目添加到集合中。

  1. Your Data class must have a parameter less constructor, otherwise XmlSerializer will never be able to create instance of Data.
  2. Instead of storing DataCoollection, you should store and retrieve Data[], it's easier without having to do anything else.
  3. While storing, you can call ToArray method to get Data[] and use typeof(Data[]) for serializer.
  4. While reading you can read the array and add items into your collection.

这篇关于为什么我的ObservableCollection序列化不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 09:38