カスタム オブジェクトのコレクションをアプリケーション設定に保存しようとしています。
この関連する質問の助けを借りて、現在私が持っているものは次のとおりです。
// 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 Designer で、ブラウザー ボタンを使用して PeopleHolder タイプのプロパティを追加し、スコープを「ユーザー」に設定しました。Save() メソッドは正常に完了したようで、エラー メッセージは表示されませんが、アプリケーションを再起動するたびに設定が保持されません。
上記のコードには示されていませんが、カスタムコレクションではなく、文字列を永続化できます(SOに関する他の同様の質問で、デバッグ中に設定を保存できないバージョン番号に問題があることに気付いたので、ルールを適用したい可能性のある犯人としてそれをアウト.)
何か案は?私はこれを行うための非常に簡単な方法があると確信しています:)。
ご協力いただきありがとうございます!