Sdílet prostřednictvím


AttributeCallback-Delegat

Aktualisiert: November 2007

Wird aufgerufen, wenn für einen Typ Attribute erforderlich sind.

Namespace:  Microsoft.Windows.Design.Metadata
Assembly:  Microsoft.Windows.Design (in Microsoft.Windows.Design.dll)

Syntax

'Declaration
Public Delegate Sub AttributeCallback ( _
    builder As AttributeCallbackBuilder _
)
'Usage
Dim instance As New AttributeCallback(AddressOf HandlerMethod)
public delegate void AttributeCallback(
    AttributeCallbackBuilder builder
)
public delegate void AttributeCallback(
    AttributeCallbackBuilder^ builder
)
JScript unterstützt keine Delegaten.

Parameter

Hinweise

Verwenden Sie das AttributeCallback-Delegat, wenn Sie eine große Attributtabelle auffüllen. Mit dem Rückrufmuster wird das Auffüllen der Attributtabelle verzögert, bis vom Designer die Metadaten für einen Typ zur Entwurfszeit benötigt werden.

Beispiele

Im folgenden Codebeispiel ist dargestellt, wie eine Attributtabelle mithilfe der AttributeCallbackBuilder-Klasse erstellt und aufgefüllt wird.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows;

using Microsoft.Windows.Design.Features;
using Microsoft.Windows.Design.Metadata;

namespace CustomControlLibrary.VisualStudio.Design
{
    // Container for any general design-time metadata to initialize.
    // Designers look for a type in the design-time assembly that 
    // implements IRegisterMetadata. If found, designers instantiate 
    // this class and call its Register() method automatically.
    internal class Metadata : IRegisterMetadata
    {
        // Called by the designer to register any design-time metadata.
        public void Register()
        {
            AttributeTableBuilder builder = new AttributeTableBuilder();

            // Build the attribute table by using the AttributeCallbackBuilder 
            // class. The attribute table is not populated until the designer
            // needs it, which is more efficient for large attribute tables.
            builder.AddCallback(
                typeof(Button), 
                delegate(AttributeCallbackBuilder callbackBuilder)
            {
                callbackBuilder.AddCustomAttributes(
                    new DefaultPropertyAttribute("Content"));

                // Apply the ReadOnlyAttribute to the Background property 
                // of the Button class.
                callbackBuilder.AddCustomAttributes(
                    "Background",
                    new ReadOnlyAttribute(true));

                PropertyDescriptorCollection properties =
                    TypeDescriptor.GetProperties(typeof(Button));
                PropertyDescriptor pd = properties["Foreground"];
                callbackBuilder.AddCustomAttributes(
                    pd,
                    new ReadOnlyAttribute(true));

                callbackBuilder.AddCustomAttributes(
                   Button.WidthProperty,
                   new TypeConverterAttribute(typeof(LengthConverter)),
                   new DescriptionAttribute("This is the width"));

                MemberInfo[] members = typeof(Button).GetMember("Height");
                callbackBuilder.AddCustomAttributes(
                    members[0],
                    new ReadOnlyAttribute(true));
            });

            MetadataStore.AddAttributeTable(builder.CreateTable());
        }
    }
}

Siehe auch

Referenz

Microsoft.Windows.Design.Metadata-Namespace

AttributeTableBuilder

AddCallback

AttributeTable

Weitere Ressourcen

Metadatenspeicher

Exemplarische Vorgehensweise: Erstellen eines Entwurfszeitadorners

WPF-Designer-Erweiterbarkeit