麻烦节省对象的应用程序设置的集合应用程序、节省、麻烦、对象

2023-09-03 00:54:45 作者:静看°一季花开花落

我想存储在应用程序设置自定义对象的集合。

I'm trying to store a collection of custom objects in the Application Settings.

通过从一些帮助this相关的问题,这里是我目前有:

With some help from this related question, here is what I currently have:

// implementing ApplicationSettingsBase so this shows up in the Settings designer's 
// browse function
public class PeopleHolder : ApplicationSettingsBase
{
    [UserScopedSetting()]
    [SettingsSerializeAs(System.Configuration.SettingsSerializeAs.Xml)]
    public ObservableCollection<Person> People { get; set; }
}


[Serializable]
public class Person
{
    public String FirstName { get; set; }
}

public MainWindow()
{
    InitializeComponent();

    // AllPeople is always null, not persisting
    if (Properties.Settings.Default.AllPeople == null)
    {
        Properties.Settings.Default.AllPeople = new PeopleHolder()
            {
                People = new ObservableCollection<Person> 
                    { 
                        new Person() { FirstName = "bob" },
                        new Person() { FirstName = "sue" },
                        new Person() { FirstName = "bill" }
                    }
            };
        Properties.Settings.Default.Save();
    }
    else
    {
        MessageBox.Show(Properties.Settings.Default.AllPeople.People.Count.ToString());
    }
}

在Settings.Settings设计师,我通过浏览器按钮添加型PeopleHolder的属性,范围设置为用户。在Save()方法似乎成功完成,没有任何错误信息,但每次我重新启动应用程序的设置都不会保留。

In the Settings.Settings Designer I added property of type PeopleHolder via the browser button, and set the scope to 'User'. The Save() method seems to complete successfully, there are no error messages, but every time I restart the application settings are not persisted.

尽管在code上面没有显示,我能坚持字符串,只是不是我的自定义集合(我注意到,在其他类似的问题上所以有时会用版本号一个问题,prevents保存设置在调试时,所以我想排除作为可能的罪魁祸首。)

Though not shown in the code above, I am able to persist Strings, just not my custom collection (I noticed in other similar questions on SO there can sometimes be a problem with version numbers which prevents save the settings while debugging so I want to rule out that as the possible culprit.)

任何想法?我敢肯定,有一个很简单的方法来做到这一点,我只是缺少的:)

Any ideas? I'm sure there is a very simple way to do this that I'm just missing :).

感谢您的帮助!

推荐答案

我想它了感谢this问题!

所建议的这个问题,我说这Settings.Designer.cs:

As suggested in that question I added this to Settings.Designer.cs:

    [global::System.Configuration.UserScopedSettingAttribute()]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    public ObservableCollection<Person> AllPeople
    {
        get
        {
            return ((ObservableCollection<Person>)(this["AllPeople"]));
        }
        set
        {
            this["AllPeople"] = value;
        }
    }

然后把所有我需要的是下面的code:

And then all I needed was the following code:

[Serializable]
public class Person
{
    public String FirstName { get; set; }
}

public MainWindow()
{
    InitializeComponent();

    // this now works!!
    if (Properties.Settings.Default.AllPeople == null)
    {
        Properties.Settings.Default.AllPeople = new ObservableCollection<Person> 
        { 
            new Person() { FirstName = "bob" },
            new Person() { FirstName = "sue" },
            new Person() { FirstName = "bill" }
        };
        Properties.Settings.Default.Save();
    }
    else
    {
        MessageBox.Show(Properties.Settings.Default.AllPeople.People.Count.ToString());
    }
}