2011-10-07 61 views
9

我試圖在應用程序設置中存儲自定義對象的集合。在應用程序設置中保存對象集合時遇到問題

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()方法似乎成功完成,沒有錯誤消息,但每次重新啓動應用程序設置都不會持久。

雖然沒有在上面的代碼中顯示,但我能夠堅持字符串,而不是我的自定義集合(我在其他類似的問題上注意到,所以有時會出現版本號的問題,從而無法在調試時保存設置我想排除這是可能的罪魁禍首。)

任何想法?我確信有一個非常簡單的方法來做到這一點,我只是想念:)。

感謝您的幫助!

回答

10

我想通了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()); 
    } 
} 
+0

謝謝你這樣簡單的解決方案。對我很好! – Seekeer

+0

+1 - 像魅力一樣工作。謝謝。 :) –

+0

+1是的,它也適用於我在同樣的情況。但是,也許你已經想出了更優雅的解決方案,而不是參與黑客設置settings.Designer.cs? – beduin

3

如果添加ObservableCollection<People>到您的自己的代碼,但是指定「屬性」命名空間,則可以在不更改設置的情況下進行此更改.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(我可能不需要那樣做)。

我在整個解決方案/答案中看到的唯一不足就是您​​無法再使用Project - > Properties對話框更改應用程序配置設置。這樣做會使嚴重將您的設置轉換爲字符串並損壞XML標記,從而搞亂了您的新設置。

因爲我想使用一個系統範圍的配置文件而不是用戶特定的文件,我還將global::System.Configuration.UserScopedSettingAttribute()]更改爲[global::System.Configuration.ApplicationScopedSetting()]。我在課堂上離開了set accesser,但我知道它實際上並沒有保存。

感謝您的回答!它使我的代碼變得更整潔,更易於管理。

+0

非常感謝這篇技巧。我已經失去了對Settings.Designer.cs所做的更改 – thowa

相關問題