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

    你可能感兴趣的文章
    NN&DL4.1 Deep L-layer neural network简介
    查看>>
    NN&DL4.3 Getting your matrix dimensions right
    查看>>
    NN&DL4.8 What does this have to do with the brain?
    查看>>
    nnU-Net 终极指南
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    NO 157 去掉禅道访问地址中的zentao
    查看>>
    no available service ‘default‘ found, please make sure registry config corre seata
    查看>>
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>
    No resource identifier found for attribute 'srcCompat' in package的解决办法
    查看>>