2012-10-01 101 views
1

我複製並粘貼Example on MSDN自定義配置部

我改變了部分decleration從

<section 
    name="pageAppearance" 
    type="Samples.AspNet.PageAppearanceSection" 
    allowLocation="true" 
    allowDefinition="Everywhere" 
    /> 

<section 
    name="pageAppearance" 
    type="Samples.AspNet.PageAppearanceSection, Samples.AspNet " /> 

以外,所有的碼是完全相同的。出於某種原因,msdn的源代碼只顯示默認值。 有人可以幫我弄清楚爲什麼它不會讀取配置部分,謝謝。

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <!-- Configuration section-handler declaration area. --> 
    <configSections> 
     <sectionGroup name="pageAppearanceGroup"> 
      <section 
     name="pageAppearance" 
     type="Samples.AspNet.PageAppearanceSection, Samples.AspNet " 
       allowLocation="true" 
       allowDefinition="Everywhere"/> 
     </sectionGroup> 
    </configSections> 
    <startup> 
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 
    </startup> 
    <pageAppearanceGroup> 
     <pageAppearance remoteOnly="true"> 
      <font name="TimesNewRoman" size="18"/> 
      <color background="000000" foreground="FFFFFF"/> 
     </pageAppearance> 
    </pageAppearanceGroup> 
</configuration> 

using System; 
using System.Collections; 
using System.Text; 
using System.Configuration; 
using System.Xml; 

namespace Samples.AspNet 
{ 
    public class PageAppearanceSection : ConfigurationSection 
    { 
     // Create a "remoteOnly" attribute. 
     [ConfigurationProperty("remoteOnly")] 
     public Boolean RemoteOnly 
     { 
      get 
      { 
       return (Boolean)this["remoteOnly"]; 
      } 
      set 
      { 
       this["remoteOnly"] = value; 
      } 
     } 

     // Create a "font" element. 
     [ConfigurationProperty("font")] 
     public FontElement Font 
     { 
      get 
      { 
       return (FontElement)this["font"]; 
      } 
      set 
      { this["font"] = value; } 
     } 

     // Create a "color element." 
     [ConfigurationProperty("color")] 
     public ColorElement Color 
     { 
      get 
      { 
       return (ColorElement)this["color"]; 
      } 
      set 
      { this["color"] = value; } 
     } 
    } 

    // Define the "font" element 
    // with "name" and "size" attributes. 
    public class FontElement : ConfigurationElement 
    { 
     [ConfigurationProperty("name")] 
     [StringValidator(InvalidCharacters = "[email protected]#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)] 
     public String Name 
     { 
      get 
      { 
       return (String)this["name"]; 
      } 
      set 
      { 
       this["name"] = value; 
      } 
     } 

     [ConfigurationProperty("size", DefaultValue = "12", IsRequired = false)] 
     [IntegerValidator(ExcludeRange = false, MaxValue = 24, MinValue = 6)] 
     public int Size 
     { 
      get 
      { return (int)this["size"]; } 
      set 
      { this["size"] = value; } 
     } 
    } 

    // Define the "color" element 
    // with "background" and "foreground" attributes. 
    public class ColorElement : ConfigurationElement 
    { 
     [ConfigurationProperty("background", DefaultValue = "FFFFFF", IsRequired = true)] 
     [StringValidator(InvalidCharacters = "[email protected]#$%^&*()[]{}/;'\"|\\GHIJKLMNOPQRSTUVWXYZ", MinLength = 6, MaxLength = 6)] 
     public String Background 
     { 
      get 
      { 
       return (String)this["background"]; 
      } 
      set 
      { 
       this["background"] = value; 
      } 
     } 

     [ConfigurationProperty("foreground", DefaultValue = "000000", IsRequired = true)] 
     [StringValidator(InvalidCharacters = "[email protected]#$%^&*()[]{}/;'\"|\\GHIJKLMNOPQRSTUVWXYZ", MinLength = 6, MaxLength = 6)] 
     public String Foreground 
     { 
      get 
      { 
       return (String)this["foreground"]; 
      } 
      set 
      { 
       this["foreground"] = value; 
      } 
     } 

    } 

} 
+0

我可能錯過了一些東西,但爲什麼這個標籤爲'winforms'? – jp2code

+1

@ jp2code我使用winforms標記了它,因爲這是msdn上的示例基礎的註釋,它對我幫助最大。 – gh9

+0

你編譯的程序集的文件名是什麼? –

回答

1

關於你的情況,我調試了一下,發現有什麼問題。 只是註釋以下行:

[StringValidator(InvalidCharacters = "[email protected]#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)] 

因此類FontElement應該是這個樣子:

// Define the "font" element 
// with "name" and "size" attributes. 
public class FontElement : ConfigurationElement 
{ 

    [ConfigurationProperty("name")] 
    public String Name 
    { 
     get 
     { 
      return (String)this["name"]; 
     } 
     set 
     { 
      this["name"] = value; 
     } 
    } 

    [ConfigurationProperty("size", DefaultValue = "12", IsRequired = false)] 
    [IntegerValidator(ExcludeRange = false, MaxValue = 24, MinValue = 6)] 
    public int Size 
    { 
     get 
     { return (int)this["size"]; } 
     set 
     { this["size"] = value; } 
    } 
} 

,你準備好去!

+0

它仍然沒有,我測試它的方式是將RemoteOnly設置爲true,然後輸出該布爾值。輸出仍然是錯誤的。 – gh9

+0

好吧,我再次測試,它的工作原理應該如此。讓我後我的工作配置 –

+0

這裏是: <?XML版本= 「1.0」 編碼= 「UTF-8」> <配置> <段名= 「pageAppearance」 類型= 「Samples.AspNet.PageAppearanceSection,Samples.AspNet」 allowLocation = 「真」 allowDefinition的= 「無處不在」/>