Trouble saving a collection of objects in Application Settings

≯℡__Kan透↙ 提交于 2019-12-17 19:40:55

问题


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

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());
    }
}

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.

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 :).

Thanks for your help!


回答1:


I figured it out thanks to this question!

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;
        }
    }

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());
    }
}



回答2:


If you add the ObservableCollection<People> to your own code, but specify the "Properties" namespace, you can make this change without altering the settings.Designer.cs:

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

Please note, I changed the accessibility of the Settings class to be public. (I probably didn't need to do that).

The only downside I saw in this whole solution/answer is that you are no longer able to make changes to the application configuration settings using the Project -> Properties dialog. Doing so will seriously mess up your new settings by converting you setting to a string and mangling your XML tags.

Because I wanted to use a single system-wide configuration file instead of a user-specific file, I also changed the global::System.Configuration.UserScopedSettingAttribute()] to [global::System.Configuration.ApplicationScopedSetting()]. I left the set accesser in the class, but I know that it doesn't actually save.

Thanks for the answer! It makes my code a whole lot cleaner and easier to manage.



来源:https://stackoverflow.com/questions/7681957/trouble-saving-a-collection-of-objects-in-application-settings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!