0

我正在尝试创建一个自定义设置类来存储一些用户范围的设置。几个月前我这样做了,效果很好,但我不记得所有细节,也无法再访问源代码。这是我到目前为止所拥有的:

自定义设置.cs

internal sealed partial class CustomSettings : global::System.Configuration.ApplicationSettingsBase
{
    private static CustomSettings defaultInstance = ((CustomSettings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new CustomSettings())));

    public static CustomSettings Default
    {
        get
        {
            return defaultInstance;
        }
    }

    [global::System.Configuration.UserScopedSettingAttribute()]
    public global::System.Collections.Generic.List<global::LearningProject.Types.Product> Products
    {
        get
        {
            return ((global::System.Collections.Generic.List<global::LearningProject.Types.Product>)(this["Products"]));
        }
        set
        {
            this["Products"] = value;
        }
    }
}

产品.cs

[Serializable]
public class Product
{
    [XmlAttribute("Name")]
    public string Name { get; set; }

    [XmlAttribute("Id")]
    public int Id { get; set; }
}

这是App.config文件,其中包含一个示例Product

<configuration>
<configSections>
    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="LearningProject.Settings1" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
      <section name="LearningProject.CustomSettings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
    </sectionGroup>
</configSections>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
  <userSettings>
        <LearningProject.CustomSettings>
          <setting name="Products" serializeAs="Xml">
            <ArrayOfProduct>
              <Product>
                <Name>Product1</Name>
                <Id>ProductID</Id>
              </Product>
            </ArrayOfProduct>
          </setting>
        </LearningProject.CustomSettings>
    </userSettings>
</configuration>

现在,null当我尝试获取以下列表时,我得到了Products

List<Product> products = CustomSettings.Default.Products;

这意味着我做错了什么(可能是xml属性?)或者我错过了一个步骤。另请注意,Settings1.settings(由 VS 生成)已经存在,我的CustomSettings类只是将方法添加到默认的自动生成的设置文件中。我只是想知道是否有人这样做过。任何帮助表示赞赏。

4

1 回答 1

1

在 app.config 中替换

<setting name="Products" serializeAs="Xml">
  <ArrayOfProduct>
    <Product>
      <Name>Product1</Name>
      <Id>ProductID</Id>
    </Product>
  </ArrayOfProduct>
</setting>

<setting name="Products" serializeAs="Xml">
  <value>
    <ArrayOfProduct>
      <Product Name="Product1" Id="1" />
    </ArrayOfProduct>
  </value>
</setting>

我是如何意识到这一点的

CustomSettings.Default.Products = new List<Product>();
CustomSettings.Default.Products.Add(new Product() { Id = 1, Name = "Product1" });
CustomSettings.Default.Save();

现在本地配置 (%appdatalocal%\project_name..\user.config) 包含正确的值

于 2013-06-26T06:20:48.397 回答