2009-10-26 164 views
3

無效,我有以下的代碼,但我得到以下編譯錯誤:屬性上聲明類型

屬性「WebPartStorage」不在此聲明類型有效。它只對'property,indexer'聲明有效。

AND

屬性'FriendlyName'在此聲明類型中無效。它只對'property,indexer'聲明有效。

我已經從MSDN文章修改了我的代碼:http://msdn.microsoft.com/en-us/library/dd584174(office.11).aspx。有沒有人有任何想法我做錯了,這是造成這個錯誤?

[Category("Custom Properties")] 
    [DefaultValue(RegionEnum.None)] 
    [WebPartStorage(Storage.Shared)] 
    [FriendlyName("Region")] 
    [Description("Select a value from the dropdown list.")] 
    [Browsable(true)] 
    protected RegionEnum _Region; 
    public RegionEnum Region 
    { 
     get 
     { 
      return _Region; 
     } 
     set 
     { 
      _Region = value; 
     } 
    } 

回答

7

您似乎已將該屬性附加到該字段;屬性始終堅持下一步事情(在這種情況下,該領域)。您應該重新排序,以便它們堅持屬性而不是字段。

BTW;受保護的領域很少是一個好主意(他們應該是私人的);但特別是如果該財產是公開的:重點是什麼?

protected RegionEnum _Region; 
[Category("Custom Properties")] 
[DefaultValue(RegionEnum.None)] 
[WebPartStorage(Storage.Shared)] 
[FriendlyName("Region")] 
[Description("Select a value from the dropdown list.")] 
[Browsable(true)] 
public RegionEnum Region 
{ 
    get { return _Region; } 
    set { _Region = value; } 
} 
1

該消息告訴你,不是嗎?您正嘗試將該屬性設置爲一個字段,但僅對索引器和屬性有效。

protected RegionEnum _Region; 

[Category("Custom Properties")] 
[DefaultValue(RegionEnum.None)] 
[Description("Select a value from the dropdown list.")] 
[Browsable(true)] 
[WebPartStorage(Storage.Shared)] 
[FriendlyName("Region")] 
public RegionEnum Region 
{ 
    get 
    { 
     return _Region; 
    } 
    set 
    { 
     _Region = value; 
    } 
} 
0

希望你有using Microsoft.SharePoint.WebPartPages;,對嗎?