存储UI设置的最佳做法?

问题描述 投票:11回答:7

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

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

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

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

c# wpf settings appsettings
7个回答
13
投票

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

Environment.SpecialFolder.ApplicationData

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

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


10
投票

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

<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序列化程序来保留它。 此方法使您可以从版本迁移到版本。 虽然功能更强大,但此方法需要更多代码。


4
投票

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

xaml文件:

<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; }
        }
    }
}

2
投票

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


1
投票

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


1
投票

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


1
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.