Clase ResourcesAttribute

Deriva de Object.Attribute y se usa para especificar cómo se debe traducir una propiedad.

Jerarquía de la herencia

System.Object
  System.Attribute
    Microsoft.SharePoint.WebPartPages.ResourcesAttribute

Espacio de nombres:  Microsoft.SharePoint.WebPartPages
Ensamblado:  Microsoft.SharePoint (en Microsoft.SharePoint.dll)

Sintaxis

'Declaración
<AttributeUsageAttribute(AttributeTargets.Property Or AttributeTargets.Field)> _
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public NotInheritable Class ResourcesAttribute _
    Inherits Attribute
'Uso
Dim instance As ResourcesAttribute
[AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field)]
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public sealed class ResourcesAttribute : Attribute

Comentarios

En tiempo de ejecución para la clase ResourcesAttribute busca el CustomPropertyToolPart . Si se encuentra, la CustomPropertyToolPart itera a través del método LoadResource (debe invalidar este método) con los valores de recursos desde el ResourcesAttribute. Si no se encuentra el recurso, se crea la interfaz de usuario de CustomPropertyToolPart con los valores predeterminados. De lo contrario, se crea la interfaz de usuario de CustomPropertyToolPart con los valores traducidos. Microsoft .NET Framework sólo proporcionará los valores de localización para la etiqueta de propiedad, descripciones y rótulo de categorías. No se traducirá el nombre de la propiedad base. Si no se define la clase ResourcesAttribute , la interfaz de usuario de CustomPropertyToolPart volverá a los metadatos no localizada (es decir, CategoryAttribute, DescriptionAttribute, FriendlyNameAttributey el nombre interno de la propiedad).

Ejemplos

En el ejemplo de código siguiente se muestra cómo localizar la propiedad personalizada cuando se muestra en la interfaz de usuario de 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);
        }
    }
}

Seguridad para subprocesos

Los miembros static (Shared en Visual Basic) públicos de este tipo son seguros para subprocesos. No se garantiza que los miembros de instancias sean seguros para los subprocesos.

Vea también

Referencia

Miembros ResourcesAttribute

Espacio de nombres Microsoft.SharePoint.WebPartPages