演练:提供自定义设计时元数据

[本文档仅供预览,在以后的发行版中可能会发生更改。包含的空白主题用作占位符。]

适用于 Visual Studio 的 WPF 设计器 可以为不同的设计器提供自定义设计时元数据。 例如,可以针对您的自定义控件的不同的设计体验在 Visual Studio 和 expression blend。 注册与设计器,您的自定义设计时元数据 IProvideAttributeTable 实现接口并调用一个 AddCustomAttributes 方法。

本演练演示如何为 WPF 或 Silverlight 自定义控件库提供自定义设计时实现。 在本演练中,您将执行下列任务:

  • 创建一个 WPF 自定义控件库项目。

  • 创建设计时元数据的一个单独的程序集。

完成后,您将了解如何为 WPF 或 Silverlight 自定义控件提供自定义设计时元数据。

备注

您看到的对话框和菜单命令可能会与 " 帮助 " 中的描述不同具体取决于您现用的设置或版本。若要更改设置,请选择在 工具 菜单的 导入和导出设置 。有关更多信息,请参见 Visual Studio 设置

系统必备

您需要以下组件来完成本演练:

  • Visual Studio 2012 RC.

创建自定义控件

第一步是为 WPF 自定义控件创建项目。

创建自定义控件

  1. 创建新 WPF 自定义控件库名为 TailspinToysControlLibrary的项目在 Visual Basic 或 Visual C#。

    CustomControl1 的代码在代码编辑器中打开。

    备注

    如果您的自定义设计体验只需要面向 expression blend 4 和 Visual Studio 2010,请将项目的目标框架设置为 .NET framework 4。如果您的自定义设计体验必须面向 expression blend 3, expression blend 4,因此, Visual Studio 2010,请将项目的目标框架设置为 .NET framework 3.5。

  2. 解决方案资源管理器,将代码文件的名称更改为 TailspinToysControl.cs 或 TailspinToysControl.vb。 如果此项目请求的出现消息框是要执行重命名所有引用,单击

  3. 打开项目属性并选择 生成 (在 Visual Basic 中编译 ) 选项。

  4. 将项目的输出路径设置为 “bin \”。

  5. 生成解决方案。

创建设计时元数据程序集

设计时代码在特定元数据程序集中部署。 对于本演练,自定义元数据。 Visual Studio 和 expression blend 在名为 TailspinToysControlLibrary.Design 的程序集支持和部署。 有关命名程序集的更多信息,请 部署自定义控件和设计时程序集参见。

创建设计时元数据程序集

  1. 在 Visual Basic 中添加个新的类库名为 TailspinToysControlLibrary.Design 的项目或 Visual c# 为解决方案。

  2. 将项目的输出路径设置为 “ \ TailspinToysControlLibrary \ bin \”。 这在同一文件夹使控件的程序集和元数据程序集,使设计器的元数据发现。

  3. 添加对下列 WPF 和 XAML 程序集。

    • PresentationCore

    • PresentationFramework

    • System.Xaml

    • WindowsBase

  4. 添加对以下 WPF Designer 程序集。 

    • Microsoft.Windows.Design.Extensibility

    • Microsoft.Windows.Design.Interaction

    备注

    如果您安装了 expression blend,您可能会发现两组中增加的 WPF Designer 程序集引用 " 对话框。选择两个程序集一起安装 Visual Studio,位于 $ (VSInstallDir) \ Common7 \ IDE \ PublicAssemblies 文件夹。

    备注

    如果 WPF Designer 程序集未出现在 " 添加引用 " 对话框。单击 浏览 选项并导航到程序集位于 $/(VSInstallDir) \ Common7 \ IDE \ PublicAssemblies 文件夹。

  5. 添加对 TailspinToysControlLibrary 项目。

  6. 解决方案资源管理器,将 Class1 代码文件的名称更改为 RegisterMetadata.cs 或 RegisterMetadata.vb。

  7. 用下面的代码替换自动生成的代码。 将自定义设计时特性附加到 TailspinToysControl 类的此代码创建 AttributeTable

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    using Microsoft.Windows.Design;
    using Microsoft.Windows.Design.Features;
    using Microsoft.Windows.Design.Metadata;
    
    using TailspinToysControlLibrary;
    
    // The ProvideMetadata assembly-level attribute indicates to designers
    // that this assembly contains a class that provides an attribute table. 
    [assembly: ProvideMetadata(typeof(TailspinToysControlLibrary.Design.RegisterMetadata))]
    namespace TailspinToysControlLibrary.Design
    {
        internal class RegisterMetadata : IProvideAttributeTable
        {
            // Called by the designer to register any design-time metadata. 
            public AttributeTable AttributeTable
            {
                get
                {
                    AttributeTableBuilder builder = new AttributeTableBuilder();
    
                    // Set ToolboxBrowsableAttribute to true to display your custom control 
                    // in the Toolbox and in the Choose Items... dialog box. 
                    builder.AddCustomAttributes(typeof(TailspinToysControl), new ToolboxBrowsableAttribute(true));
    
                    return builder.CreateTable();
                }
            }
        }
    }
    
  8. 生成解决方案。

后续步骤

请参见

任务

演练:为工具箱图标提供元数据

演练:为控件创建自定义工具箱图标

参考

AttributeTable

概念

工具箱图标

部署自定义控件和设计时程序集

提供设计时元数据

其他资源

选择工具箱项, WPF 元素