博客
关于我
VS2010 Extension实践(1)
阅读量:446 次
发布时间:2019-03-06

本文共 3846 字,大约阅读时间需要 12 分钟。

Visual Studio 2010 Extension开发指南

在Visual Studio 2010中开发扩展,近年来越来越受关注。然而,很多开发者在尝试探索相关文档时,往往会遇到信息碎片化和完整性不足的问题。为了让自己更好地掌握这一领域,我决定从零开始,结合VS IDE提供的模板和Visual Studio Blog上的技术分享,通过Reflector反编译和手动编码,逐步实现一个功能性强大的代码编辑器扩展。

本文将详细介绍如何创建一个基本的VS Extension,并结合实际开发经验,分享实现过程中的关键点和解决方案。

1. 获取VS2010 SDK

首先,需要下载Visual Studio 2010 SDK Beta2版本。通过安装VS2010 SDK,可以为扩展开发提供必要的基础支持。

2. 使用Editor Text Adornment模板

创建VS Extension工程时,可以选择Editor Text Adornment模板。这一模板能够帮助快速创建一个基本的扩展框架。对于不熟悉VS Extension开发的开发者,建议参考Quan To的技术文章获取更多详细信息。

3. 实现 TextViewCreationListener

通过Editor Text Adornment模板,工程会自动生成TextViewCreationListener类。这个类实现了IWpfTextViewCreationListener接口,并通过MEF导出IWpfTextViewCreationListener对象。

[TextViewRole("DOCUMENT")][Export(typeof(IWpfTextViewCreationListener))][ContentType("text")]internal sealed class PETextViewCreationListener : IWpfTextViewCreationListener{    void IWpfTextViewCreationListener.TextViewCreated(IWpfTextView textView)    {        // ...     }}

4. 导出AdornmentLayerDefinition

为了实现浮动工具栏,需要在Extension中导出AdornmentLayerDefinition,并通过Order Attribute定制Adornment层的显示位置和显示顺序。

[Name("QuickToolbarAdornmentLayer")][Order(After = "Text")][Export(typeof(AdornmentLayerDefinition))]public AdornmentLayerDefinition QuickToolbarLayerDefinition{    get; set;}

5. 获取AdornmentLayer

在代码中,通过this._textView.GetAdornmentLayer("QuickToolbarAdornmentLayer")获取所创建的AdornmentLayer。

6. 处理TextView事件

在IEWpfTextViewCreationListener.TextViewCreated方法中,通过获取IWpfTextView对象,挂钩Closed、LayoutChanged、MouseHovered、SelectionChanged等事件,响应用户操作。

7. 导入IEditorOperationsFactoryService

为了支持代码编辑功能,需要导入IEditorOperationsFactoryService,并在IEWpfTextViewCreationListener.TextViewCreated中通过其GetEditorOperations方法获取IEditorOperations实例。

[Import]internal IEditorOperationsFactoryService EditorOperationsFactoryService{    get; set;}

8. 实现工具栏界面

创建一个UserControl组件,内置ToolBar控件。通过IEWpfTextView的SelectionChanged事件,判断何时何地显示工具栏。

9. 工具栏显示条件

通过MayBeAdornmentShowCondition方法,根据用户操作(如选择文本区域或鼠标悬停)决定是否显示工具栏。

private void MayBeAdornmentShowCondition(){    if (!this._textView.Selection.IsEmpty)    {        // ...         if (this._mustHaveAdornmentDisplayed)        {            // ...             Canvas.SetTop(this._adornmentUI, top);            Canvas.SetLeft(this._adornmentUI, left);            // ...        }        else        {            this._mustHaveAdornmentDisplayed = false;            this._adornmentLayer.RemoveAdornmentsByTag(this._adornmentTag);        }    }    else    {        this._mustHaveAdornmentDisplayed = false;        this._adornmentLayer.RemoveAdornmentsByTag(this._adornmentTag);    }}

10. 处理工具栏事件

通过RenderSelectionPopup方法,在工具栏显示时,判断是否需要添加Adornment层元素,并启动定时器。

private void RenderSelectionPopup(){    if (this._mustHaveAdornmentDisplayed)    {        IAdornmentLayerElement element = null;        try        {            element = this._adornmentLayer.Elements.First((IAdornmentLayerElement ile) => ile.Tag.ToString() == this._adornmentTag);        }        catch (InvalidOperationException)        { }        if (element == null)        {            this._adornmentLayer.AddAdornment(this._textView.Selection.SelectedSpans[0], this._adornmentTag, this._adornmentUI);        }        this._timer.Stop();        this._timer.Start();    }}

11. 事件处理

通过selection_SelectionChanged方法,响应用户选择变化,决定是否显示工具栏。

private void selection_SelectionChanged(object sender, EventArgs e){    this._fromMouseHover = false;    this.MayBeAdornmentShowCondition();}

12. 关闭事件处理

确保在IEWpfTextView的Closed事件中取消所有挂钩的事件,以避免内存泄漏或逻辑错误。

13. 编译与打包

完成开发后,通过编译工具将项目打包为VSIX文件,完成扩展的部署和安装。

14. 功能亮点

目前实现的主要功能包括:

  • 当用户在代码编辑器中选择文本区域并将鼠标悬停在文本上,QuickToolbar会以半透明的方式浮现在文本旁边。

  • 当鼠标悬停在QuickToolbar区域时,QuickToolbar会变为不透明,其按钮和功能将响应鼠标操作。

  • 支持以下功能:

    • 剪切(Cut)
    • 复制(Copy)
    • 粘贴(Paste)
    • 删除(Delete)
    • 减小缩进(Decrease Indent)
    • 增加缩进(Increase Indent)
    • 注释代码(Comment)
    • 取消注释(Uncomment)
    • 等等

    15. 安装与使用

    VSIX文件及源代码下载链接请参考相关开发者论坛获取。通过安装VSIX文件,可以方便地在Visual Studio中体验和使用该扩展功能。

    转载地址:http://hmufz.baihongyu.com/

    你可能感兴趣的文章
    nodejs 运行CMD命令
    查看>>
    Nodejs+Express+Mysql实现简单用户管理增删改查
    查看>>
    nodejs+nginx获取真实ip
    查看>>
    nodejs-mime类型
    查看>>
    NodeJs——(11)控制权转移next
    查看>>
    NodeJS、NPM安装配置步骤(windows版本)
    查看>>
    NodeJS、NPM安装配置步骤(windows版本)
    查看>>
    nodejs下的express安装
    查看>>
    nodejs与javascript中的aes加密
    查看>>
    nodejs中Express 路由统一设置缓存的小技巧
    查看>>
    nodejs中express的使用
    查看>>
    Nodejs中搭建一个静态Web服务器,通过读取文件获取响应类型
    查看>>
    Nodejs中的fs模块的使用
    查看>>
    NodeJS使用淘宝npm镜像站的各种姿势
    查看>>
    NodeJs入门知识
    查看>>
    nodejs包管理工具对比:npm、Yarn、cnpm、npx
    查看>>
    NodeJs单元测试之 API性能测试
    查看>>
    nodejs图片转换字节保存
    查看>>
    nodejs在Liunx上的部署生产方式-PM2
    查看>>
    nodejs基于art-template模板引擎生成
    查看>>