本演练创建两个 " 工具选项 " 页、简单的页和自定义页。 简单页使用 " 属性 " 网格检查和设置属性,并且,自定义页提供自己的用户界面 (UI)。 托管包框架 (MPF)支持两种方案,将很少其他代码。
若要保存这些属性以及从设置文件还原它们,请执行以下步骤,然后 演练:创建设置类别参见。
MPF 提供两个类来帮助您创建 " 工具选项 " 页、 Package 类和 DialogPage 类。 您创建 VSPackage 为这些页面提供容器通过 subclassing 包类。 您创建每个通过派生工具选项卡页从 DialogPage 类。
在本演练中,将创建 " 工具选项 " 页并使用它们显示和更改应用程序选项。 要从承载的 VSPackage 检索选项值。 使用自动化,则从其他 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++。
创建工具选项网格页
在本节中,您使用 Visual Studio 包 " 项目模板创建简单的工具选项 " 属性 " 网格。 使用此网格显示和更改属性的值。
创建 VSPackage 项目
创建新的 Visual Studio 包 " 项目命名 MyToolsOptions。
有关如何创建托管 VSPackage 的更多信息,请参见 演练:使用 Visual Studio 创建包模板的菜单命令。
在 选择一种编程语言 页上,选择 Visual Basic 或 Visual C#。
在 选择 VSPackage 选项 页上,选择 菜单命令。
在 命令选项 页,请将单个 Visual Basic 或 Visual C# 的 命令名 到 获取内部选项 VB 或 获取内部选项 cs ,设置 命令 ID 到 cmdidGetIntOpt,然后单击 完成。
创建工具选项 " 属性 " 网格
在代码编辑器中打开 MyToolsOptionsPackage 文件。
添加以下命名空间引用。
Imports System.ComponentModel
using System.ComponentModel;
声明 OptionPageGrid 类并从 DialogPage派生。
Public Class OptionPageGrid Inherits DialogPage
public class OptionPageGrid : DialogPage
应用 ClassInterfaceAttribute 于 OptionPageGrid 类:
<ClassInterface(ClassInterfaceType.AutoDual)> <CLSCompliant(False), ComVisible(True)> Public Class OptionPageGrid Inherits DialogPage
[ClassInterface(ClassInterfaceType.AutoDual)] [CLSCompliant(false), ComVisible(true)] public class OptionPageGrid : DialogPage
这将创建允许 Visual Studio 自动使用 GetAutomationObject 访问类的公共成员编程的 COM 双重接口。
应用 ProvideOptionPageAttribute 于 VSPackage 类分配给类选项类别和选项卡页名称。 OptionPageGrid。
<PackageRegistration(UseManagedResourcesOnly:=True), InstalledProductRegistration("#110", "#112", "1.0", IconResourceID:=400), ProvideMenuResource("Menus.ctmenu", 1), Guid(GuidList.guidMyToolsOptionsPkgString)> <ProvideOptionPage(GetType(OptionPageGrid), "My Category", "My Grid Page", 0, 0, True)> Public NotInheritable Class MyToolsOptionsPackage Inherits Package
[PackageRegistration(UseManagedResourcesOnly = true), InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400), ProvideMenuResource("Menus.ctmenu", 1), Guid(GuidList.guidMyToolsOptionsPkgString)] [ProvideOptionPage(typeof(OptionPageGrid), "My Category", "My Grid Page", 0, 0, true)] public sealed class MyToolsOptionsPackage : Package
备注
可以通过将资源 ID 本地化类别到属性 CategoryResourceID。
添加一个 OptionInteger 属性设置为 OptionPageGrid 类。
应用 CategoryAttribute 分配给属性的属性网格类别。
应用 DisplayNameAttribute 分配给属性的名称。
应用 DescriptionAttribute 分配给属性的说明。
<ClassInterface(ClassInterfaceType.AutoDual)> <CLSCompliant(False), ComVisible(True)> Public Class OptionPageGrid Inherits DialogPage Private optionValue As Integer = 256 <Category("My Category")> <DisplayName("My Option Integer")> <Description("My integer option")> Public Property OptionInteger() As Integer Get Return optionValue End Get Set(ByVal value As Integer) optionValue = value End Set End Property End Class
[ClassInterface(ClassInterfaceType.AutoDual)] [CLSCompliant(false), ComVisible(true)] public class OptionPageGrid : DialogPage { private int optionValue = 256; [Category("My Category")] [DisplayName("My Integer Option")] [Description("My integer option")] public int OptionInteger { get { return optionValue; } set { optionValue = value; } } }
备注
DialogPage 的默认实现支持具有适当的转换器或为结构或数组可以展开到属性具有适当的转换器的属性。有关转换器的列表,请参见 System.ComponentModel 命名空间。Visual Studio 扩展性示例 管理 int、 string和 System.Drawing.Size 属性。
安装和启动项目按 F5 调试模式。 这将启动在的实验项的 Visual Studio。
备注
Visual Studio 的两个版本现在处于打开状态。
在 Visual Studio,在 工具 菜单中单击 选项。
选项 对话框打开。
在树视图中,展开 我的类别 然后单击 我的网格页。
选项网格出现在右窗格中。 特性类是 我的选项,因此,属性名称与 我选项整数。 属性声明, 我的整数选项,将出现在窗格底部。
从初始值的 256 将值更改为其他。 单击 好,然后重新打开 我的网格页。 可以看到新值仍然存在。
退出 Visual Studio Exp。
创建工具选项自定义页
本节中,您将自定义 UI 创建 " 工具选项 " 页。 使用此页显示和更改属性的值。
创建工具选项自定义页
在代码编辑器中打开 MyToolsOptionsPackage 文件。
添加以下命名空间语句。
Imports System.Windows.Forms
using System.Windows.Forms;
添加 OptionPageCustom 类,因此,在 OptionPageGrid 类之前。 从 DialogPage派生新类。
Public Class OptionPageCustom Inherits DialogPage
public class OptionPageCustom : DialogPage
在 工具 菜单上,单击 创建 GUID 复制和粘贴使用注册表布局的新的 GUID 到代码,移除大括号。 添加 ClassInterfaceAttribute。 添加字符串属性, OptionString。 请注意 GUID 与该示例中的 GUID 将不同:
<ClassInterface(ClassInterfaceType.AutoDual), Guid("42C7F4D9-200D-4fe2-A093-49AFA2DDE7F8")> Public Class OptionPageCustom Inherits DialogPage Private optionValue As String = "alpha" Public Property OptionString() As String Get Return optionValue End Get Set(ByVal value As String) optionValue = value End Set End Property End Class
[ClassInterface(ClassInterfaceType.AutoDual)] [Guid("1D9ECCF3-5D2F-4112-9B25-264596873DC9")] public class OptionPageCustom : DialogPage { private string optionValue = "alpha"; public string OptionString { get { return optionValue; } set { optionValue = value; } } }
应用 ProvideOptionPageAttribute 于 VSPackage 类。 这将把类选项类别,并且选项称为名称。
<PackageRegistration(UseManagedResourcesOnly:=True), InstalledProductRegistration("#110", "#112", "1.0", IconResourceID:=400), ProvideMenuResource("Menus.ctmenu", 1), Guid(GuidList.guidMyToolsOptionsPkgString)> <ProvideOptionPage(GetType(OptionPageCustom), "My Category", "My Custom Page", 0, 0, True)> <ProvideOptionPage(GetType(OptionPageGrid), "My Category", "My Grid Page", 0, 0, True)> Public NotInheritable Class MyToolsOptionsPackage Inherits Package
[PackageRegistration(UseManagedResourcesOnly = true), InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400), ProvideMenuResource("Menus.ctmenu", 1), Guid(GuidList.guidMyToolsOptionsPkgString)] [ProvideOptionPage(typeof(OptionPageCustom), "My Category", "My Custom Page", 0, 0, true)] [ProvideOptionPage(typeof(OptionPageGrid), "My Category", "My Grid Page", 0, 0, true)] public sealed class MyToolsOptionsPackage : Package
将名为 MyUserControl 的用户控件添加到项目中。
用户控件在设计视图中打开。
备注
如果看到警告,请重新生成解决方案并重新打开 MyUserControl.cs。这是已知问题。
添加一个 文本框 控件添加到用户控件。 在 属性 窗口,请在工具栏上,单击 事件 按钮,然后双击 离开 事件。
代码编辑器打开显示新的事件处理程序。
添加一个公共 OptionsPage 字段,与控件类的一个 Initialize 方法,并更新事件处理程序的主体如下所示:
Public Class MyUserControl Friend optionsPage As OptionPageCustom Public Sub Initialize() TextBox1.Text = optionsPage.OptionString End Sub Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As EventArgs) _ Handles TextBox1.Leave optionsPage.OptionString = TextBox1.Text End Sub End Class
public partial class MyUserControl : UserControl { public MyUserControl() { InitializeComponent(); } internal OptionPageCustom optionsPage; public void Initialize() { textBox1.Text = optionsPage.OptionString; } private void textBox1_Leave(object sender, EventArgs e) { optionsPage.OptionString = textBox1.Text; } }
optionsPage 字段包含对父 OptionPageCustom 实例。 Initialize 方法显示在 文本框的 OptionString 。 事件处理程序编写 文本框 的当前值为 OptionString 的,当焦点离开 文本框。
在包代码文件中,添加 OptionPageCustom.Window 属性的重写以 OptionPageCustom 类创建,初始化,并返回 MyUserControl实例。 下面是一个示例:
<Browsable(False)> <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> Protected Overrides ReadOnly Property Window() As IWin32Window Get Dim page As New MyUserControl() page.optionsPage = Me page.Initialize() Return page End Get End Property
[Browsable(false)] [DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden)] protected override IWin32Window Window { get { MyUserControl page = new MyUserControl(); page.optionsPage = this; page.Initialize(); return page; } }
安装和启动项目按 F5 调试模式。 这将启动新的 Visual Studio 实例。
备注
Visual Studio 的两个版本此时已打开。
在新 Visual Studio 实例,请单击 工具 菜单上的并单击 选项。
选项 对话框打开。
在左窗格中的树视图,展开 我的类别 然后单击 我的自定义页。
从初始值更改 OptionString 的值, alpha,给其他人。 单击 好,然后重新打开 我的自定义页。 可以看到新值仍然存在。
关闭新 Visual Studio 实例。
从承载的 VSPackage 中访问的选项
在本节中,您获取一个选项的值从承载关联的 " 工具选项 " 页的 VSPackage 中。 同一方法来获取所有的公共属性值。
访问与承载 VSPackage 的选项
在包文件,用以下行替换 MyToolsOptions.MenuItemCallback 方法的主体:
Private Sub MenuItemCallback(ByVal sender As Object, ByVal e As EventArgs) Dim page As OptionPageGrid = CType(GetDialogPage(GetType(OptionPageGrid)), OptionPageGrid) MessageBox.Show(String.Format(CultureInfo.CurrentCulture, "OptionInteger: {0}", page.OptionInteger)) End Sub
private void MenuItemCallback(object sender, EventArgs e) { OptionPageGrid page = (OptionPageGrid)GetDialogPage(typeof(OptionPageGrid)); MessageBox.Show(string.Format(CultureInfo.CurrentCulture, "OptionInteger: {0}", page.OptionInteger)); }
此代码调用 GetDialogPage 创建或检索 OptionPageGrid 实例。 OptionPageGrid 随后调用 LoadSettingsFromStorage 填充其选项,是公共属性。
安装和启动项目按键盘快捷方式调试模式, F5。 这将启动 Visual Studio的新实例。
备注
Visual Studio 的两个版本此时已打开。
在 工具 菜单上,单击 获取内部选项。
消息框显示 OptionInteger的当前值。
退出新 Visual Studio 实例。
使用自动化的访问的选项
在本节中,您使用自动获取一个选项的值从所有 VSPackage 中或外接程序。 同一方法来获取所有的公共属性值。
访问使用自动化的选项
右击 " 解决方案资源管理器 的 MyToolsOptions 解决方案并添加新 Visual Studio 包 " 项目命名 AnotherPackage。
有关如何创建托管 VSPackage 的更多信息,请参见 演练:使用 Visual Studio 创建包模板的菜单命令。
在 选择一种编程语言 页上,选择 Visual Basic 或 Visual C#。
在 选择 VSPackage 选项 页上,选择 菜单命令。
在 命令选项 页,请将 命令名 获取外部选项”,然后单击 完成。
模板创建托管项目, AnotherPackage。
右击 AnotherPackage 项目节点然后单击 设置为启动项目。
添加以下命名空间对在 AnotherPackage 项目的 VsPkg 代码文件:
Imports EnvDTE Imports System.Windows.Forms
using EnvDTE; using System.Windows.Forms;
用以下行替换 AnotherPackage.MenuItemCallback 方法的主体:
Private Sub MenuItemCallback(ByVal sender As Object, ByVal e As EventArgs) Dim env As DTE = CType(GetService(GetType(DTE)), DTE) Dim props As EnvDTE.Properties = env.Properties("My Category", "My Grid Page") Dim value As Integer = CInt(props.Item("OptionInteger").Value) MessageBox.Show("OptionInteger: " & value.ToString()) End Sub
private void MenuItemCallback(object sender, EventArgs e) { DTE env = (DTE)GetService(typeof(DTE)); EnvDTE.Properties props = env.get_Properties("My Category", "My Grid Page"); int n = (int)props.Item("OptionInteger").Value; MessageBox.Show("OptionInteger: " + n); }
此代码调用服务来获取 DTE 对象, Visual Studio 自动化模型的根对象。 Properties 返回 My Category.MyGridPage的属性集合,包括所有公共属性。 Item 方法选择 OptionInteger 从集合。
安装和启动项目按键盘快捷方式调试模式, F5。 这将启动 Visual Studio的新实例。
备注
Visual Studio 的两个版本此时已打开。
在 工具 菜单上,单击 获取外部选项。
消息框显示 OptionInteger的当前值。