博客
关于我
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/

    你可能感兴趣的文章
    nginx 配置 单页面应用的解决方案
    查看>>
    nginx 配置https(一)—— 自签名证书
    查看>>
    nginx 配置~~~本身就是一个静态资源的服务器
    查看>>
    Nginx 配置清单(一篇够用)
    查看>>
    Nginx 配置解析:从基础到高级应用指南
    查看>>
    nginx+php的搭建
    查看>>
    nginx+tomcat+memcached
    查看>>
    nginx+Tomcat性能监控
    查看>>
    nginx+uwsgi+django
    查看>>
    Nginx-http-flv-module流媒体服务器搭建+模拟推流+flv.js在前端html和Vue中播放HTTP-FLV视频流
    查看>>
    nginx-vts + prometheus 监控nginx
    查看>>
    Nginx下配置codeigniter框架方法
    查看>>
    Nginx之二:nginx.conf简单配置(参数详解)
    查看>>
    Nginx代理websocket配置(解决websocket异常断开连接tcp连接不断问题)
    查看>>
    Nginx代理初探
    查看>>
    nginx代理地图服务--离线部署地图服务(地图数据篇.4)
    查看>>
    Nginx代理外网映射
    查看>>
    Nginx代理模式下 log-format 获取客户端真实IP
    查看>>
    Nginx代理静态资源(gis瓦片图片)实现非固定ip的url适配网络环境映射ip下的资源请求解决方案
    查看>>
    Nginx代理静态资源(gis瓦片图片)实现非固定ip的url适配网络环境映射ip下的资源请求解决方案
    查看>>