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

    你可能感兴趣的文章
    NPM使用前设置和升级
    查看>>
    npm入门,这篇就够了
    查看>>
    npm切换到淘宝源
    查看>>
    npm切换源淘宝源的两种方法
    查看>>
    npm前端包管理工具简介---npm工作笔记001
    查看>>
    npm包管理深度探索:从基础到进阶全面教程!
    查看>>
    npm升级以及使用淘宝npm镜像
    查看>>
    npm发布包--所遇到的问题
    查看>>
    npm发布自己的组件UI包(详细步骤,图文并茂)
    查看>>
    npm和package.json那些不为常人所知的小秘密
    查看>>
    npm和yarn清理缓存命令
    查看>>
    npm和yarn的使用对比
    查看>>
    npm如何清空缓存并重新打包?
    查看>>
    npm学习(十一)之package-lock.json
    查看>>
    npm安装 出现 npm ERR! code ETIMEDOUT npm ERR! syscall connect npm ERR! errno ETIMEDOUT npm ERR! 解决方法
    查看>>
    npm安装crypto-js 如何安装crypto-js, python爬虫安装加解密插件 找不到模块crypto-js python报错解决丢失crypto-js模块
    查看>>
    npm安装教程
    查看>>
    npm报错Cannot find module ‘webpack‘ Require stack
    查看>>
    npm报错Failed at the node-sass@4.14.1 postinstall script
    查看>>
    npm报错fatal: Could not read from remote repository
    查看>>