本演练演示托管 VSPackage 如何能够提供动态配置为 ToolboxItem 对象支持。
备注
操作的最简单方式将自定义控件添加到工具箱将使用随 Visual Studio 10 SDK 的工具箱控件模板。本主题使用高级工具箱开发相关。
有关使用该模板创建的工具箱控件的更多信息,请参见 如何:创建使用 windows 窗体的一个工具箱控件 和 如何:使用 WPF 创建一个工具箱控件。
本演练将引导您完成以下步骤:
使用 ToolboxItemAttribute 和 ToolboxBitmapAttribute 类,添加和正确注册在 VSPackage 对象的所有 工具箱 控件。
创建以下两个控件并将每个的图标。 工具箱:
声明一个关联的默认 ToolboxItem的控件。
声明一个关联的自定义 工具箱 项从 ToolboxItem 类派生的控件。
编写 IConfigureToolboxItem的实现并将其注册通过执行以下操作:
定义 IConfigureToolboxItem 从类派生并指定 ToolboxItem 实例此实现将操作筛选器类。
应用 ProvideToolboxItemConfigurationAttribute,引用筛选器类,在实现 VSPackage 中 Package 类的类。
注册 VSPackage 作为 ToolboxItem 对象提供程序通过应用 ProvideToolboxItemsAttribute 于实现 VSPackage 中 Package 类的类。
在包加载时,使用反射生成 VSPackage 提供所有 ToolboxItem 对象的列表。
创建 ToolboxInitialized 和 ToolboxUpgraded 事件的处理程序。 这样做是为了确保 VSPackage 中 ToolboxItem 对象能正确加载。
实现在 VSPackage 中的命令强制 工具箱的再预装。
系统必备
若要完成本演练,您必须安装 Visual Studio 2010 SDK。
备注
有关 Visual Studio SDK 的更多信息,请参见 扩展 Visual Studio 概述。若要查找有关中所列如何下载 Visual Studio SDK,请 Visual Studio Extensibility Developer Center 参见 MSDN 网站上。
Visual Studio 包 " 项目模板的位置
Visual Studio 包 " 项目模板可以在 新项目 对话框的三个不同位置找到:
在 Visual Basic 扩展性下。 该项的默认语言是 Visual Basic。
在 C# 扩展性下。 该项目的默认语言是 C#。
在其他项下键入扩展性。 该项的默认语言是 C++。
创建托管 VSPackage
完成以下过程创建托管 VSPackage。
创建托管 VSPackage 提供工具箱项
创建名为 ItemConfiguration的 VSPackage。 有关更多信息,请参见 演练:使用 Visual Studio 创建包模板的菜单命令。
在 Visual Studio 包 模板中,添加一个菜单命令。
为 Visual Basic 或 初始化 ItemConfigurationCS 的命令 初始化 ItemConfigurationVB Visual C# 中。
对于 Visual Basic,添加以下命名空间到导入的命名空间列表在生成的项:
Company.ItemConfiguration
系统
System.ComponentModel
System.Drawing
System.Windows.Forms
添加对 System.Drawing.Design .NET framework 组件。
如果您遵循多个语言的本演练,您必须更新项目包含生成的程序集。
消除 Visual Basic 和 Visual C# Vspackage
为 Visual Basic:
打开项目属性,然后选择 应用程序 选项。
更改程序集名称。 ItemConfigurationVB,并更改默认命名空间。 Company.ItemConfigurationVB。
选择 引用 选项。
更新导入的命名空间列表。
移除 Company.ItemConfiguration。
添加 Company.ItemConfigurationVB。
为 Visual C#:
打开项目属性,然后选择 应用程序 选项。
更改程序集名称。 ItemConfigurationCS,并更改默认命名空间。 Company.ItemConfigurationCS。
在代码编辑器中打开 ItemConfigurationPackage 类。
若要使用重构工具给现有的命名空间重命名,右击现有的命名空间名称, ItemConfiguration,指向 重构,然后单击 重命名。 更改该名称。 ItemConfigurationCS。
保存所有更改。
测试生成的代码
生成并启动在 Visual Studio 的实验项的 VSPackage。
在 工具 菜单上,单击 初始化项目配置 VB 或 初始化项目配置 cs。
执行此打开包含文本指示的消息框包的菜单项处理程序。
关闭 Visual Studio的实验版本。
创建工具箱控件
在本节中,您创建和注册用户控件, Control1,声明一个关联的默认 工具箱 项目。 还将创建并注册第二个用户控件、 Control2与关联的自定义 工具箱 项目, Control2_ToolboxItem,从 ToolboxItem 类派生。 有关如何创作 windows 窗体控件和 ToolboxItem 类的更多信息,请参见 设计时开发 Windows 窗体控件。
创建默认和自定义工具箱项
使用命令在 演练:自动上载的工具箱项 创建默认 工具箱 项目和自定义 工具箱 项目,如下所示。
完成此过程, “创建将使用在默认 ToolboxItem 的 工具箱 控件”。更新 DescriptionAttribute 引用此项目。
Control1 类的生成的代码应类似于以下代码。
' Set the display name and custom bitmap to use for this item. ' The build action for the bitmap must be "Embedded Resource". <DisplayName("ToolboxMember 1 VB")> _ <Description("Custom toolbox item from package ItemConfiguration.")> _ <ToolboxItem(True)> _ <ToolboxBitmap(GetType(Control1), "Control1.bmp")> _ Public Class Control1 Public Sub New() InitializeComponent() Button1.Text = Me.Name + " Button" End Sub Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) _ Handles Button1.Click MessageBox.Show("Hello world from " & Me.ToString()) End Sub End Class
using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace Company.ItemConfigurationCS { // Set the display name and custom bitmap to use for this item. // The build action for the bitmap must be "Embedded Resource". [DisplayName("ToolboxMember 1 CS")] [Description("Custom toolbox item from package ItemConfiguration.")] [ToolboxItem(true)] [ToolboxBitmap(typeof(Control1), "Control1.bmp")] public partial class Control1 : UserControl { public Control1() { InitializeComponent(); button1.Text = this.Name + " Button"; } private void button1_Click(object sender, EventArgs e) { MessageBox.Show("Hello world from " + this.ToString()); } } }
完成此过程, “创建一个 工具箱 控件用于自定义 ToolboxItem 派生的类”。更新 DescriptionAttribute 引用此项目。
Control2 和 Control2_ToolboxItem 类的生成的代码应类似于以下代码。
Imports System.Drawing.Design Imports System.Globalization ' Set the display name and custom bitmap to use for Me item. ' The build action for the bitmap must be "Embedded Resource". ' Also declare a custom toolbox item implementation. <DisplayName("ToolboxMember 2 VB")> _ <Description("Custom toolbox item from package ItemConfiguration.")> _ <ToolboxItem(GetType(Control2_ToolboxItem))> _ <ToolboxBitmap(GetType(Control2), "Control2.bmp")> _ Public Class Control2 Public Sub New() InitializeComponent() Button1.Text = Me.Name + " Button" End Sub Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) _ Handles Button1.Click MessageBox.Show("Hello world from " & Me.ToString()) End Sub End Class <Serializable()> _ Friend Class Control2_ToolboxItem Inherits ToolboxItem Public Sub New(ByVal toolType As Type) MyBase.New(toolType) End Sub Public Overrides Sub Initialize(ByVal toolType As Type) If Not toolType.Equals(GetType(Control2)) Then Throw New ArgumentException( _ String.Format(CultureInfo.CurrentCulture, _ "The {0} constructor argument must be of type {1}.", _ Me.GetType().FullName, GetType(Control2).FullName)) End If MyBase.Initialize(toolType) End Sub End Class
using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; using System.Drawing.Design; using System.Globalization; namespace Company.ItemConfigurationCS { // Set the display name and custom bitmap to use for this item. // The build action for the bitmap must be "Embedded Resource". // Also declare a custom toolbox item implementation. [DisplayName("ToolboxMember 2 CS")] [Description("Custom toolbox item from package ItemConfigurationPackage.")] [ToolboxItem(typeof(Control2_ToolboxItem))] [ToolboxBitmap(typeof(Control2), "Control2.bmp")] public partial class Control2 : UserControl { public Control2() { InitializeComponent(); button1.Text = this.Name + " Button"; } private void button1_Click(object sender, EventArgs e) { MessageBox.Show("Hello world from " + this.ToString()); } } [Serializable()] internal class Control2_ToolboxItem : ToolboxItem { public Control2_ToolboxItem(Type toolType) : base(toolType) { } public override void Initialize(Type toolType) { if (!toolType.Equals(typeof(Control2))) { throw new ArgumentException( string.Format(CultureInfo.CurrentCulture, "The {0} constructor argument must be of type {1}.", this.GetType().FullName, typeof(Control2).FullName)); } base.Initialize(toolType); } } }
保存文件。
嵌入图标位图
向每个控件的 ToolboxBitmapAttribute 属性指定图标与该控件。 创建两个控件的位图,并将它们命名为如下所示:
Control1.bmp, Control1 类的。
Control2.bmp, Control2 类的。
嵌入工具箱项的位图图标
添加一个新的位图到项目,如下所示:
在 解决方案资源管理器,右击 VSPackage 项目,指向 添加,然后单击 新项目。
在 添加新项目 对话框中,选择 " 位图文件,并输入位图的名称。
使用位图编辑器创建 16∝16 图标,如下所示。
在**“视图”菜单上,单击“属性窗口”**。
在 属性 窗口中,将 高度 和 宽度 为 16。
使用编辑器生成图标图像。
在 解决方案资源管理器,右击该项目,然后单击 属性。
设置 生成操作 属性设置为 嵌入资源。
保存所有打开的文件。
添加一个动态工具箱配置提供程序
在本节中,将创建并注册 IConfigureToolboxItem 实现接口的类。 实例化和 Visual Studio 集成开发环境 (ide) 用于此类 (IDE)配置 工具箱 控件。
备注
由于 Visual Studio 环境实例化 IConfigureToolboxItem的实现的实例,请不要实现该类的 IConfigureToolboxItem 接口 VSPackage 中实现 Package 。
创建和注册工具箱控件配置对象
在 解决方案资源管理器,请将类添加到 ItemConfiguration 项目,并将其命名为 ToolboxConfig。
添加以下命名空间语句添加到文件中。
Imports Microsoft.VisualStudio.Shell Imports System.Collections Imports System.ComponentModel Imports System.Drawing.Design Imports System.Reflection Imports System.Runtime.InteropServices Imports System.Security.Permissions
using Microsoft.VisualStudio.Shell; using System.Collections; using System.ComponentModel; using System.Drawing.Design; using System.Reflection; using System.Runtime.InteropServices; using System.Security.Permissions;
确保 ToolboxConfig 类是 public 并实现 IConfigureToolboxItem 接口。
应用 GuidAttribute 和 ProvideAssemblyFilterAttribute 于 ToolboxConfig 类.
对于 Visual Basic,请使用 “ItemConfigurationVB, Version=*, Culture=*, PublicKeyToken=*”的程序集名称。
对于 Visual C#,请使用 “ItemConfigurationCS, Version=*, Culture=*, PublicKeyToken=*”的程序集名称。
这样做工作限制 ToolboxConfig 类在程序集提供包含当前 VSPackage 中 ToolboxItem 对象。
<ProvideAssemblyFilter("ItemConfigurationVB, Version=*, Culture=*, PublicKeyToken=*")> _ <Guid("4DDC7895-442A-45e7-82E7-4E85F243C321")> _ Public Class ToolboxConfig Implements IConfigureToolboxItem
[ProvideAssemblyFilter("ItemConfigurationCS, Version=*, Culture=*, PublicKeyToken=*")] [Guid("E6832593-BF07-4de1-AA0F-7F9B94887DB8")] public class ToolboxConfig : IConfigureToolboxItem
可以通过单击 创建 GUID 生成 GUID 在 工具 菜单。 选择的 与方形大括号的格式,单击 复制,然后将其粘贴到代码的 GUID。 更改关键字 GUID 到 Guid。
执行 IConfigureToolboxItem 接口的 ConfigureToolboxItem 方法,以便该方法在两 ToolboxItem 类、 Control1 和 Control2只操作。
ConfigureToolboxItem 的实现应用 ToolboxItemFilterAttribute 实例于两 ToolboxItem 对象,这样:
由 Control1 实现的 ToolboxItem 只有在 工具箱 设计器的处理 UserControl 对象。
由 Control2 实现的 ToolboxItem 不在 工具箱 设计器的处理 UserControl 对象。
<PrincipalPermission(SecurityAction.Demand)> Public Sub _ ConfigureToolboxItem(ByVal item As ToolboxItem) _ Implements IConfigureToolboxItem.ConfigureToolboxItem If item Is Nothing Then Return ' Create a filter for the Toolbox. Dim newFilter As ToolboxItemFilterAttribute If GetType(Control1).ToString() = item.TypeName Then ' For Control1, only show it when editing a UserControl. newFilter = New ToolboxItemFilterAttribute( _ "System.Windows.Forms.UserControl", _ ToolboxItemFilterType.Require) ElseIf GetType(Control2).ToString() = item.TypeName Then ' For Control2, only show it when not editing a UserControl. newFilter = New ToolboxItemFilterAttribute( _ "System.Windows.Forms.UserControl", _ ToolboxItemFilterType.Prevent) Else ' Don't apply a filter to other classes. Return End If item.Filter = CType(New ToolboxItemFilterAttribute() {newFilter}, ICollection) End Sub
[PrincipalPermission(SecurityAction.Demand)] public void ConfigureToolboxItem(ToolboxItem item) { if (null == item) return; // Create a filter for the Toolbox. ToolboxItemFilterAttribute newFilter; if (typeof(Control1).ToString() == item.TypeName) { // For Control1, only show it when editing a UserControl. newFilter = new ToolboxItemFilterAttribute( "System.Windows.Forms.UserControl", ToolboxItemFilterType.Require); } else if (typeof(Control2).ToString() == item.TypeName) { // For Control2, only show it when not editing a UserControl. newFilter = new ToolboxItemFilterAttribute( "System.Windows.Forms.UserControl", ToolboxItemFilterType.Prevent); } else { // Don't apply a filter to other classes. return; } item.Filter = (ICollection)(new ToolboxItemFilterAttribute[] { newFilter }); }
修改 VSPackage 实现
Visual Studio 包模板提供 VSPackage 的默认实现必须修改进行以下操作:
注册为 工具箱 项目提供程序。
注册为 VSPackage 提供动态 工具箱 控件配置的类。
使用 ToolboxService 由 VSPackage 集提供的所有 ToolboxItem 对象。
处理 ToolboxInitialized 和 ToolboxUpgraded 事件。
修改一个工具箱项提供程序的包实现在 VSPackage
在代码编辑器中打开 ItemConfigurationPackage 类。
修改 ItemConfigurationPackage 类的声明,是 Package 类实现在解决方案的。
添加以下命名空间语句添加到文件中。
Imports System.Collections Imports System.ComponentModel Imports System.Drawing.Design Imports System.Reflection
using System.Collections; using System.ComponentModel; using System.Drawing.Design; using System.Reflection;
注册 VSPackage 为了提供 工具箱 项目,应用于 ProvideToolboxItemsAttribute 包。 注册 ToolboxConfig 类作为提供 工具箱 控制动态配置,应用于 ProvideToolboxItemConfigurationAttribute 包。
' ... <PackageRegistration(UseManagedResourcesOnly:=True), _ DefaultRegistryRoot("Software\\Microsoft\\VisualStudio\\9.0"), _ InstalledProductRegistration(False, "#110", "#112", "1.0", IconResourceID:=400), _ ProvideLoadKey("Standard", "1.0", "Package Name", "Company", 1), _ ProvideMenuResource(1000, 1), _ Guid(GuidList.guidItemConfigurationPkgString)> _ <ProvideToolboxItems(1)> _ <ProvideToolboxItemConfiguration(GetType(ToolboxConfig))> _ Public NotInheritable Class DynamicToolboxMembersPackage Inherits Package
// ... [PackageRegistration(UseManagedResourcesOnly = true)] // ... [DefaultRegistryRoot("Software\\Microsoft\\VisualStudio\\9.0")] // ... [InstalledProductRegistration(false, "#110", "#112", "1.0", IconResourceID = 400)] // ... [ProvideLoadKey("Standard", "1.0", "Package Name", "Company", 1)] // ... [ProvideMenuResource(1000, 1)] [Guid(GuidList.guidItemConfigurationPkgString)] [ProvideToolboxItems(1)] [ProvideToolboxItemConfiguration(typeof(ToolboxConfig))] public sealed class DynamicToolboxMembersPackage : Package
备注
ProvideToolboxItemsAttribute 的唯一参数是 VSPackage 提供 ToolboxItem 的版本。通过更改此值,则强制 IDE 加载 VSPackage,即使它具有 ToolboxItem的早期的缓存版本。
创建两个新 private 字段。 ItemConfiguration 类,如下所示:
ArrayList 成员,名为 ToolboxItemList,保存 ItemConfiguration 类管理 ToolboxItem 对象的列表。
String,名为 CategoryTab,包含 工具箱 选项用于保存 ToolboxItem 对象 ItemConfiguration 类管理。
Private ToolboxItemList As ArrayList Private Shared ReadOnly CategoryTab As String = _ "ItemConfiguration Walkthrough VB"
private ArrayList ToolboxItemList; private static readonly string CategoryTab = "ItemConfiguration Walkthrough CS";
此修改的结果将类似于以下代码:
Imports Microsoft.VisualBasic Imports System Imports System.Diagnostics Imports System.Globalization Imports System.Runtime.InteropServices Imports System.ComponentModel.Design Imports Microsoft.Win32 Imports Microsoft.VisualStudio.Shell.Interop Imports Microsoft.VisualStudio.OLE.Interop Imports Microsoft.VisualStudio.Shell Imports System.Collections Imports System.ComponentModel Imports System.Drawing.Design Imports System.Reflection ' ... <PackageRegistration(UseManagedResourcesOnly:=True), _ DefaultRegistryRoot("Software\\Microsoft\\VisualStudio\\9.0"), _ InstalledProductRegistration(False, "#110", "#112", "1.0", IconResourceID:=400), _ ProvideLoadKey("Standard", "1.0", "Package Name", "Company", 1), _ ProvideMenuResource(1000, 1), _ Guid(GuidList.guidItemConfigurationPkgString)> _ <ProvideToolboxItems(1)> _ <ProvideToolboxItemConfiguration(GetType(ToolboxConfig))> _ Public NotInheritable Class DynamicToolboxMembersPackage Inherits Package Private ToolboxItemList As ArrayList Private Shared ReadOnly CategoryTab As String = _ "ItemConfiguration Walkthrough VB" ' ...
using System; using System.Diagnostics; using System.Globalization; using System.Runtime.InteropServices; using System.ComponentModel.Design; using Microsoft.Win32; using Microsoft.VisualStudio.Shell.Interop; using Microsoft.VisualStudio.OLE.Interop; using Microsoft.VisualStudio.Shell; using System.Collections; using System.ComponentModel; using System.Drawing.Design; using System.Reflection; namespace Company.ItemConfigurationCS { // ... [PackageRegistration(UseManagedResourcesOnly = true)] // ... [DefaultRegistryRoot("Software\\Microsoft\\VisualStudio\\9.0")] // ... [InstalledProductRegistration(false, "#110", "#112", "1.0", IconResourceID = 400)] // ... [ProvideLoadKey("Standard", "1.0", "Package Name", "Company", 1)] // ... [ProvideMenuResource(1000, 1)] [Guid(GuidList.guidItemConfigurationPkgString)] [ProvideToolboxItems(1)] [ProvideToolboxItemConfiguration(typeof(ToolboxConfig))] public sealed class DynamicToolboxMembersPackage : Package { private ArrayList ToolboxItemList; private static readonly string CategoryTab = "ItemConfiguration Walkthrough CS"; // ...
定义一个 OnRefreshToolbox 方法以处理 ToolboxInitialized 和 ToolboxUpgraded 事件。
OnRefreshToolbox 方法使用在 ToolboxItemList 字段包含 ToolboxItem 对象的列表并进行以下操作:
从由 CategoryTab 字段定义的 工具箱 选项移除所有 ToolboxItem 对象。
添加到 ToolboxItemList 字段列出所有 ToolboxItem 对象的 工具箱 选的新实例。
设置 工具箱 选项为有效的选项。
' Add new instances of all ToolboxItems to the toolbox item list. Private Sub OnRefreshToolbox(ByVal sender As Object, ByVal e As EventArgs) _ Handles Me.ToolboxInitialized, Me.ToolboxUpgraded Dim service As IToolboxService = _ TryCast(GetService(GetType(IToolboxService)), IToolboxService) Dim toolbox As IVsToolbox = _ TryCast(GetService(GetType(IVsToolbox)), IVsToolbox) ' Remove target tab and all items under it. For Each item As ToolboxItem In service.GetToolboxItems(CategoryTab) service.RemoveToolboxItem(item) Next toolbox.RemoveTab(CategoryTab) ' Recreate the target tab with the items from the current list. For Each item As ToolboxItem In ToolboxItemList service.AddToolboxItem(item, CategoryTab) Next service.SelectedCategory = CategoryTab service.Refresh() End Sub
// Add new instances of all ToolboxItems to the toolbox item list. void OnRefreshToolbox(object sender, EventArgs e) { IToolboxService service = GetService(typeof(IToolboxService)) as IToolboxService; IVsToolbox toolbox = GetService(typeof(IVsToolbox)) as IVsToolbox; // Remove target tab and all items under it. foreach (ToolboxItem item in service.GetToolboxItems(CategoryTab)) { service.RemoveToolboxItem(item); } toolbox.RemoveTab(CategoryTab); // Recreate the target tab with the items from the current list. foreach (ToolboxItem item in ToolboxItemList) { service.AddToolboxItem(item, CategoryTab); } service.SelectedCategory = CategoryTab; service.Refresh(); }
为 Visual C#,在构造函数中,注册 OnRefreshToolbox 方法作为事件处理程序 ToolboxInitialized 和 ToolboxUpgraded 动画处理。
this.ToolboxInitialized += new EventHandler(OnRefreshToolbox); this.ToolboxUpgraded += new EventHandler(OnRefreshToolbox);
修改在 ItemConfigurationPackage 的 Initialize 方法通过调用 ToolboxService 类的 GetToolboxItems 方法加载 ToolboxItemList 字段。 工具箱 服务获取当前执行的程序集中定义的所有 工具箱 项类。 工具箱 事件处理程序用于 ToolboxItemList 域加载 工具箱 项目。
将以下代码添加到 Initialize 方法的末尾。
' Use the toolbox service to get a list of all toolbox items in ' this assembly. ToolboxItemList = New ArrayList( _ ToolboxService.GetToolboxItems(Me.GetType().Assembly, "")) If ToolboxItemList Is Nothing Then Throw New ApplicationException( _ "Unable to generate a toolbox item listing for " & _ Me.GetType().FullName) End If ' Update the display name of each toolbox item in the list. Dim thisAssembly As Assembly = Me.GetType().Assembly For Each item As ToolboxItem In ToolboxItemList Dim underlyingType As Type = thisAssembly.GetType(item.TypeName) Dim attribs As AttributeCollection = _ TypeDescriptor.GetAttributes(underlyingType) Dim displayName As DisplayNameAttribute = _ TryCast(attribs(GetType(DisplayNameAttribute)), DisplayNameAttribute) If displayName IsNot Nothing AndAlso Not displayName.IsDefaultAttribute() Then item.DisplayName = displayName.DisplayName End If Next
// Use the toolbox service to get a list of all toolbox items in // this assembly. ToolboxItemList = new ArrayList( ToolboxService.GetToolboxItems(this.GetType().Assembly, "")); if (null == ToolboxItemList) { throw new ApplicationException( "Unable to generate a toolbox item listing for " + this.GetType().FullName); } // Update the display name of each toolbox item in the list. Assembly thisAssembly = this.GetType().Assembly; foreach (ToolboxItem item in ToolboxItemList) { Type underlyingType = thisAssembly.GetType(item.TypeName); AttributeCollection attribs = TypeDescriptor.GetAttributes(underlyingType); DisplayNameAttribute displayName = attribs[typeof(DisplayNameAttribute)] as DisplayNameAttribute; if (displayName != null && !displayName.IsDefaultAttribute()) { item.DisplayName = displayName.DisplayName; } }
备注
作为练习,一个可以开发测试的 VSPackage 或项目的版本一个框架和只更新,如果 VSPackage 版本的更改,或者 ToolboxItem 的版本的更改。
初始化工具箱
实现命令初始化工具箱
在 ItemConfigurationPackage 类,请更改 MenuItemCallBack 方法,即菜单项的命令处理程序。
,使用以下代码替换 MenuItemCallBack 方法的现有实现:
Private Sub MenuItemCallback(ByVal sender As Object, ByVal e As EventArgs) Dim pkg As IVsPackage = TryCast(GetService(GetType(Package)), IVsPackage) pkg.ResetDefaults(CUInt(__VSPKGRESETFLAGS.PKGRF_TOOLBOXITEMS)) End Sub
private void MenuItemCallback(object sender, EventArgs e) { IVsPackage pkg = GetService(typeof(Package)) as IVsPackage; pkg.ResetDefaults((uint)__VSPKGRESETFLAGS.PKGRF_TOOLBOXITEMS); }
生成并运行解决方案
可以执行本演练产品通过在的实验项运行 Visual Studio 的实例。
执行本演练
在 Visual Studio 中,在 生成 菜单上,单击 重新生成解决方案。
按 F5 开始 Visual Studio 第二个实例在的实验注册表项。
有关如何使用测试项的更多信息,请参见 Visual Studio 的实验实例。
单击**“工具”**菜单。
命令名为 初始化 ItemConfiguration VB 或 初始化 ItemConfiguration cs 出现在菜单顶部,与数字 1. 的图标。
创建新的 Visual C# 或 Visual Basic windows 窗体应用程序。
Form基于设计器显示。
将用户控件添加到该项目。
用户控件设计器显示。
锁已经在两个 " 设计 " 视图之间的 工具箱 和开关。
请注意 工具箱 项目可见方式,以及哪些扩展是隐藏取决于设计器具有焦点。
拖动到用户控件上的第一个 工具箱 项目添加 Control1 实例添加到用户控件。
拖动到窗体上的第二个 工具箱 项目添加 Control2 的一个实例添加到窗体。
生成 windows 应用程序。
应用程序的用户控件现在 工具箱。
添加应用程序的用户控件添加到窗体,然后重新生成应用程序并运行它。
在应用程序运行时,单击每个控件在窗体上获取关联的消息框。
对于实现的分析
由于 assemblyFilter 参数存在 ProvideAssemblyFilterAttribute 类构造函数,因此,只有在此演练中产生的程序集的 ToolboxItem 对象操作。 ToolboxConfg 类的 ConfigureToolboxItem 方法。
因此,只要, ItemConfiguration 演练 类有一个 工具箱, ToolboxConfg 类的 ConfigureToolboxItem 方法调用,因此, ToolboxItemFilterAttribute 实例是应用于 Control1 和 Control2实现的 ToolboxItem 对象。