如何将应用程序设置组织到组中?

时间:2014-12-18 08:58:50

标签: c# application-settings

我的应用程序中有几十个设置(用户范围)并且它们开始变得太多,因此我想将它们组织成组。在MSDN上link他们说:

" ...如果您已将设置组织到组中,或者在项目中添加了多个设置类,您将看到您的设置被排列为分层树。"

但是,如果我访问项目的属性,然后是设置标签,我只能设置名称,类型,范围和值。那么你在哪里按组组织设置?作为替代方案,您如何创建多个设置类?是否可以在设计时管理多个设置类?

修改

根据JesseJames的建议,我创建了这个测试类:

using System;
using System.Configuration;

namespace MyApp
{
    public class PageAppearanceSection : ConfigurationSection
    {
        // Create a "remoteOnly" attribute.
        [ConfigurationProperty("testString", DefaultValue = "false", IsRequired = false)]
        public string TestString
        {
            get{ (Boolean)this["testString"]; }
            set{ this["testString"] = value;  }
        }
    }
}

我已经用这种方式修改了我的app.config:

<configSections>
    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
        <section name="MyApp.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
    </sectionGroup>
    <!--NEW ADDED SECTION-->
    <sectionGroup name="pageAppearanceGroup">
      <section 
        name="pageAppearance" 
        type="MyApp.PageAppearanceSection" 
        allowLocation="true" 
        allowDefinition="Everywhere"
      />
    </sectionGroup>
</configSections>

然而,当我想将TestString值绑定到TextBox的Text时,我进入TextBox的属性&gt; AppliactionSettings&gt; PropertyBinding我没有看到那个属性。

1 个答案:

答案 0 :(得分:1)

您可以创建自定义配置节,然后使用节类中定义的属性。

How to: Create Custom Configuration Sections Using ConfigurationSection