【问题标题】:Trouble saving a collection of objects in Application Settings在应用程序设置中保存对象集合时遇到问题
【发布时间】:2011-12-02 16:13:09
【问题描述】:

我正在尝试在应用程序设置中存储自定义对象的集合。

this related question 的帮助下,这是我目前拥有的:

// 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() 方法似乎成功完成,没有错误消息,但每次重启应用程序设置都没有持久化。

虽然上面的代码中没有显示,但我可以保留字符串,而不是我的自定义集合(我注意到在其他类似的问题中,有时版本号可能会出现问题,这会阻止在调试时保存设置,所以我想排除这可能是罪魁祸首。)

有什么想法吗?我确信有一种非常简单的方法可以做到这一点,我只是想念:)。

感谢您的帮助!

【问题讨论】:

    标签: c# .net settings.settings


    【解决方案1】:

    如果您将ObservableCollection&lt;People&gt; 添加到您自己的代码中,但指定“Properties”命名空间,您可以在不更改 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;
                }
            }
        }
    }
    

    请注意,我将 Settings 类的可访问性更改为 public(我可能不需要这样做)。

    我在整个解决方案/答案中看到的唯一缺点是您不再能够使用“项目”->“属性”对话框更改应用程序配置设置。这样做会严重将您的设置转换为字符串并破坏您的 XML 标记,从而弄乱您的新设置。

    因为我想使用单个系统范围的配置文件而不是用户特定的文件,所以我还将global::System.Configuration.UserScopedSettingAttribute()] 更改为[global::System.Configuration.ApplicationScopedSetting()]。我在课堂上留下了set 访问器,但我知道它实际上并没有保存。

    感谢您的回答!它使我的代码更干净,更易于管理。

    【讨论】:

    • 非常感谢这个提示。我已经丢失了对 Settings.Designer.cs 所做的更改
    【解决方案2】:

    感谢this question

    按照该问题的建议,我将其添加到 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;
            }
        }
    

    然后我只需要以下代码:

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

    【讨论】:

    • +1 是的,在同样的情况下它也适用于我。但也许你想出了更优雅的解决方案,不涉及黑客 settings.Designer.cs?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-12
    • 1970-01-01
    • 2018-03-18
    • 1970-01-01
    相关资源
    最近更新 更多