12

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

  • 扩展器状态
  • 菜单订单
  • 调整属性
  • ETC...

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

你做了什么来存储大量的 ui 用户设置?

4

7 回答 7

13

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

Environment.SpecialFolder.ApplicationData

将其存储为 xml“首选项”文件,这样如果它被损坏,就不会那么难以访问和更改。

到目前为止,这对我们来说比注册表效果要好得多,如果有任何东西损坏或需要重置,它会更干净、更容易清除。

于 2008-10-29T13:56:31.930 回答
10

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

<Window ...
    xmlns:p="clr-namespace:UserSettings.Properties"
    Height="{Binding Source={x:Static p:Settings.Default}, Path=Height, Mode=TwoWay}" 
    Width="{Binding Source={x:Static p:Settings.Default}, Path=Width, Mode=TwoWay}" 
    Left="{Binding Source={x:Static p:Settings.Default}, Path=Left, Mode=TwoWay}" 
    Top="{Binding Source={x:Static p:Settings.Default}, Path=Top, Mode=TwoWay}">

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

问题在于,如果您的应用程序很大,它很快就会变得一团糟。

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

于 2008-10-29T18:19:12.133 回答
4

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

xml文件:

<Window ...
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:p="clr-namespace:MyApp"
    Height="{Binding Source={x:Static p:MyAppSettings.Default}, Path=MainWndHeight, Mode=TwoWay}"
    Width="{Binding Source={x:Static p:MyAppSettings.Default}, Path=MainWndWidth, Mode=TwoWay}"
    Left="{Binding Source={x:Static p:MyAppSettings.Default}, Path=MainWndLeft, Mode=TwoWay}"
    Top="{Binding Source={x:Static p:MyAppSettings.Default}, Path=MainWndTop, Mode=TwoWay}"
    ...

和源文件:

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; }
        }
    }
}
于 2010-02-09T06:41:53.317 回答
2

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

于 2008-10-29T13:57:40.460 回答
1

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

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

于 2008-10-29T17:26:07.123 回答
1

似乎由于某种原因正在失去人气;但注册表一直是此类设置的合适位置。

于 2008-10-29T13:52:08.910 回答
1

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

于 2008-10-29T13:58:49.523 回答