ResourcesAttribute 类
派生自Object.Attribute并用于指定应该如何本地化属性。
继承层次结构
System.Object
System.Attribute
Microsoft.SharePoint.WebPartPages.ResourcesAttribute
命名空间: Microsoft.SharePoint.WebPartPages
程序集: Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)
语法
声明
<AttributeUsageAttribute(AttributeTargets.Property Or AttributeTargets.Field)> _
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public NotInheritable Class ResourcesAttribute _
Inherits Attribute
用法
Dim instance As ResourcesAttribute
[AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field)]
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public sealed class ResourcesAttribute : Attribute
备注
在运行时, CustomPropertyToolPart查找ResourcesAttribute类。如果找到, CustomPropertyToolPart循环ResourcesAttribute中的资源值的LoadResource方法 (必须覆盖此方法)。如果找不到资源, CustomPropertyToolPart用户界面将生成的默认值。否则,使用本地化值构建CustomPropertyToolPart用户界面。Microsoft.NET Framework 将仅为提供本地化值属性标签、 说明和分类标签。不进行本地化的基本属性名称。如果未定义ResourcesAttribute类, CustomPropertyToolPart用户界面将返回到非本地化元数据 (即, CategoryAttribute、 DescriptionAttribute、 FriendlyNameAttribute和属性的内部名称)。
示例
下面的代码示例演示如何本地化自定义属性时CustomPropertyToolPart用户界面中显示。
'----------------------------------------------------------------------
'
' Code Summary: How to localize your custom property when displayed in the Property Grid.
' FriendlyName, Description, and Category are localized.
'----------------------------------------------------------------------
Imports System
Imports System.IO
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.ComponentModel
Imports Microsoft.SharePoint.WebPartPages
Imports Microsoft.SharePoint
Imports System.Xml.Serialization
Imports System.Collections.Specialized
Imports System.Collections
Imports System.Resources
Imports System.Reflection
Imports System.Drawing
Imports System.Globalization
Imports System.Runtime.InteropServices
Namespace WebControlLibrary4
'/ <summary>
'/ Summary description for WPLocalizedResource.
'/ </summary>
<GuidAttribute("9ED54F84-A89D-4fcd-A854-44251E925C55"),
XmlRoot([Namespace] := "foo"),
ToolboxData("<{0}:WPLocalizedResource
runat=server></{0}:WPLocalizedResource>")> _
Public Class WPLocalizedResource
Inherits Microsoft.SharePoint.WebPartPages.WebPart
Private Const c_MyText_Localized As String = "My Localized Property"
Private _myText_Localized As String
Public Sub New()
_myText_Localized = c_MyText_Localized
End Sub
'RESOURCES LOCALIZATION
'Property that is localized. Use the ResourceAttibute.
'[ResourcesAttribute (PropertyNameID=1, CategoryID=2, DescriptionID=3)]
<ResourcesAttribute("1", "2", "3"), Category("Cool Properties"),
DefaultValue(c_MyText_Localized),
WebPartStorage(Storage.Personal),
FriendlyNameAttribute("My Text Localized"),
Description("MyText Localized")> _
Public Property MyText_Localized() As String
Get
Return _myText_Localized
End Get
Set
_myText_Localized = value
End Set
End Property
Public Overrides Function LoadResource(myID As String) As String
Dim rm As New ResourceManager("WebControlLibrary4.Resource1",
[Assembly].GetExecutingAssembly())
Return rm.GetString(myID)
End Function
'RENDER THE WEBPART
Protected Overrides Sub RenderWebPart(output As HtmlTextWriter)
output.Write(("<br>Localized Resource Property Value: " +
Me.MyText_Localized))
End Sub
End Class
End Namespace
//---------------------------------------------------------------------
//
// Code Summary: How to localize your custom property when displayed in the Property Grid.
// FriendlyName, Description, and Category are localized.
//---------------------------------------------------------------------
using System;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint;
using System.Xml.Serialization;
using System.Collections.Specialized;
using System.Collections;
using System.Resources;
using System.Reflection;
using System.Drawing;
using System.Globalization;
using System.Runtime.InteropServices;
namespace WebControlLibrary4
{
/// <summary>
/// Summary description for WPLocalizedResource.
/// </summary>
[GuidAttribute("9ED54F84-A89D-4fcd-A854-44251E925C55")]
[XmlRoot(Namespace="foo")]
[ToolboxData("<{0}:WPLocalizedResource
runat=server></{0}:WPLocalizedResource>")]
public class WPLocalizedResource :
Microsoft.SharePoint.WebPartPages.WebPart
{
const string c_MyText_Localized = "My Localized Property";
private string _myText_Localized;
public WPLocalizedResource()
{
_myText_Localized = c_MyText_Localized;
}
//RESOURCES LOCALIZATION
//Property that is localized. Use the ResourceAttibute.
//[ResourcesAttribute (PropertyNameID=1, CategoryID=2, DescriptionID=3)]
[ResourcesAttribute ("1","2","3")]
[Category("Cool Properties")]
[DefaultValue(c_MyText_Localized),
WebPartStorage(Storage.Personal)]
[FriendlyNameAttribute("My Text Localized"),
Description("MyText Localized")]
public string MyText_Localized
{
get
{
return _myText_Localized;
}
set
{
_myText_Localized = value;
}
}
public override string LoadResource (string myID)
{
ResourceManager rm = new ResourceManager
("WebControlLibrary4.Resource1",
Assembly.GetExecutingAssembly());
return rm.GetString(myID);
}
//RENDER THE WEBPART
protected override void RenderWebPart(HtmlTextWriter output)
{
output.Write("<br>Localized Resource Property Value: " +
this.MyText_Localized);
}
}
}
线程安全性
该类型的任何公共 静态 (已共享 在 Visual Basic 中) 成员都是线程安全的。不保证任何实例成员都是线程安全的。