存储UI设置的最佳做法?

我们目前正在计划一个更大的WPF LoB应用程序,我想知道其他人认为是存储大量UI设置的最佳实践,例如

  • 扩张国
  • 菜单订单
  • 调整属性
  • 等等…

我不喜欢使用提供的SettingsProvider(即App.config文件)拥有数十个存储值的想法,尽管它可以用于使用自定义SettingsProvider将其存储在嵌入式数据库中。 能够使用某种数据绑定也是一个问题。 有没有人有同样的问题?

你做了什么来存储很多用户设置?

我们在这里存储首选项文件:

Environment.SpecialFolder.ApplicationData 

将其存储为xml“preferences”文件,这样如果它被破坏就不会那么难以改变。

到目前为止,这比我们的注册表工作得更好,如果有任何损坏或需要重置,它会更清晰,更容易爆炸。

存储UI设置的更快捷方法是使用Properties.Settings.Default系统。 可以使用它的好处是使用WPF绑定到值。 这里的例子 。 Settins会自动更新和加载。

  ... protected override void OnClosing(System.ComponentModel.CancelEventArgs e) { Settings.Default.Save(); base.OnClosing(e); } 

问题在于,如果你的应用程序很大,它就会变得很乱。

另一种解决方案(由此处某人提出)是使用ApplicationData路径将您自己的首选项存储到XML中。 在那里,您可以构建自己的设置类,并使用XML序列化程序来持久化它。 此方法使您可以从版本迁移到版本。 虽然function更强大,但此方法需要更多代码。

深入研究aogan的答案并将其与decasteljau的答案和他引用的博客文章相结合,这里有一个例子,填补了一些我不清楚的空白。

xaml文件:

  

和源文件:

 namespace MyApp { class MainWindow .... { ... protected override void OnClosing(System.ComponentModel.CancelEventArgs e) { MyAppSettings.Default.Save(); base.OnClosing(e); } } public sealed class MyAppSettings : System.Configuration.ApplicationSettingsBase { private static MyAppSettings defaultInstance = ((MyAppSettings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new MyAppSettings()))); public static MyAppSettings Default { get { return defaultInstance; } } [System.Configuration.UserScopedSettingAttribute()] [System.Configuration.DefaultSettingValueAttribute("540")] public int MainWndHeight { get { return (int)this["MainWndHeight"]; } set { this["MainWndHeight"] = value; } } [System.Configuration.UserScopedSettingAttribute()] [System.Configuration.DefaultSettingValueAttribute("790")] public int MainWndWidth { get { return (int)this["MainWndWidth"]; } set { this["MainWndWidth"] = value; } } [System.Configuration.UserScopedSettingAttribute()] [System.Configuration.DefaultSettingValueAttribute("300")] public int MainWndTop { get { return (int)this["MainWndTop"]; } set { this["MainWndTop"] = value; } } [System.Configuration.UserScopedSettingAttribute()] [System.Configuration.DefaultSettingValueAttribute("300")] public int MainWndLeft { get { return (int)this["MainWndLeft"]; } set { this["MainWndLeft"] = value; } } } } 

我们将所有存储在Isolation Storage (我们使用ClickOnce运行)。 我们有一些序列化的对象(XmlSerializer)。

似乎因某种原因失去了人气; 但是注册表一直是这些设置的合适位置。

我们使用自定义的SettingsProvider将配置信息存储在应用程序数据库的表中。 如果您已经在使用数据库,这是一个很好的解决方案。

在Chris Sells和Ian Griffiths的编程WPF中,它说

WPF应用程序的首选设置机制是.NET和VS提供的机制:System.Configuration命名空间中的ApplicationSettingBase类和内置设计器。