演练:自定义文本视图

可以通过修改其编辑器格式映射的以下属性中的任何一个自定义文本视图:

  • 指示器边距

  • 插入插入符号

  • 复盖插入符号

  • 选定的文本

  • 非活动选定的文本 (即失去了焦点) 中的选定文本

  • 可见空白。

系统必备

若要完成本演练,您必须安装 Visual Studio 2010 SDK。 有关 Visual Studio SDK 的更多信息以及下载它,请 Visual Studio 扩展性开发人员中心) 参见 MSDN 网站上。

创建一个 managed extensibility framework 项目 (MEF)

创建 MEF 项目

  1. 创建 c# 编辑分类器项目或 Visual Basic 编辑器分类器项目。 将解决方案命名为 ViewPropertyTest。

  2. 打开在 VSIX 清单编辑器中的 source.extension.vsixmanifest 文件。

  3. 确保 Content 归为包含一个 MEF 组件内容类型,而路径设置为 ViewPropertyTest.dll。

  4. 保存并关闭的 source.extension.vsixmanifest。

  5. 删除现有类文件。

定义内容类型

定义内容类型

  1. 将类文件并将其命名为 ViewPropertyModifier。

  2. 添加以下 using 指令 (在 Visual Basic 中Imports 语句)。

    Imports System
    Imports System.Collections
    Imports System.Windows
    Imports System.Windows.Media
    Imports System.ComponentModel.Composition
    Imports Microsoft.VisualStudio.Text.Classification
    Imports Microsoft.VisualStudio.Text.Editor
    Imports Microsoft.VisualStudio.Utilities
    
    using System;
    using System.Collections;
    using System.Windows;
    using System.Windows.Media;
    using System.ComponentModel.Composition;
    using Microsoft.VisualStudio.Text.Classification;
    using Microsoft.VisualStudio.Text.Editor;
    using Microsoft.VisualStudio.Utilities;
    
  3. 声明从 IWpfTextViewCreationListener继承的名为的类。 TestViewCreationListener 。 导出具有下列特性的此类:

    <Export(GetType(IWpfTextViewCreationListener)), ContentType("text"), TextViewRole(PredefinedTextViewRoles.Document)>
    Friend Class TestViewCreationListener
        Implements IWpfTextViewCreationListener
    
    [Export(typeof(IWpfTextViewCreationListener))]
    [ContentType("text")]
    [TextViewRole(PredefinedTextViewRoles.Document)]
    internal class TestViewCreationListener : IWpfTextViewCreationListener
    
  4. 此类中,导入 IEditorFormatMapService

    <Import()>
    Friend FormatMapService As IEditorFormatMapService = Nothing
    
    [Import]
    internal IEditorFormatMapService FormatMapService = null;
    

更改视图属性

更改视图属性

  • 执行 TextViewCreated 方法,以便更改视图的属性,打开后视图。 若要进行更改,对应于视图的方面要查找的第一查找 ResourceDictionary 。 然后将在资源字典中相应的属性并设置属性。 批处理调用 SetProperties 方法通过调用 BeginBatchUpdate 方法,在设置属性来 EndBatchUpdate 之前,在设置属性后。

    Public Sub TextViewCreated(ByVal textView As IWpfTextView) Implements IWpfTextViewCreationListener.TextViewCreated
    
        Dim formatMap As IEditorFormatMap = FormatMapService.GetEditorFormatMap(textView)
    
        Dim regularCaretProperties As ResourceDictionary = formatMap.GetProperties("Caret")
        Dim overwriteCaretProperties As ResourceDictionary = formatMap.GetProperties("Overwrite Caret")
        Dim indicatorMargin As ResourceDictionary = formatMap.GetProperties("Indicator Margin")
        Dim visibleWhitespace As ResourceDictionary = formatMap.GetProperties("Visible Whitespace")
        Dim selectedText As ResourceDictionary = formatMap.GetProperties("Selected Text")
        Dim inactiveSelectedText As ResourceDictionary = formatMap.GetProperties("Inactive Selected Text")
    
        formatMap.BeginBatchUpdate()
    
        regularCaretProperties(EditorFormatDefinition.ForegroundBrushId) = Brushes.Magenta
        formatMap.SetProperties("Caret", regularCaretProperties)
    
        overwriteCaretProperties(EditorFormatDefinition.ForegroundBrushId) = Brushes.Turquoise
        formatMap.SetProperties("Overwrite Caret", overwriteCaretProperties)
    
        indicatorMargin(EditorFormatDefinition.BackgroundColorId) = Colors.LightGreen
        formatMap.SetProperties("Indicator Margin", indicatorMargin)
    
        visibleWhitespace(EditorFormatDefinition.ForegroundColorId) = Colors.Red
        formatMap.SetProperties("Visible Whitespace", visibleWhitespace)
    
        selectedText(EditorFormatDefinition.BackgroundBrushId) = Brushes.LightPink
        formatMap.SetProperties("Selected Text", selectedText)
    
        inactiveSelectedText(EditorFormatDefinition.BackgroundBrushId) = Brushes.DeepPink
        formatMap.SetProperties("Inactive Selected Text", inactiveSelectedText)
    
        formatMap.EndBatchUpdate()
    End Sub
    
    public void TextViewCreated(IWpfTextView textView)
    {
        IEditorFormatMap formatMap = FormatMapService.GetEditorFormatMap(textView);
    
        ResourceDictionary regularCaretProperties = formatMap.GetProperties("Caret");
        ResourceDictionary overwriteCaretProperties = formatMap.GetProperties("Overwrite Caret");
        ResourceDictionary indicatorMargin = formatMap.GetProperties("Indicator Margin");
        ResourceDictionary visibleWhitespace = formatMap.GetProperties("Visible Whitespace");
        ResourceDictionary selectedText = formatMap.GetProperties("Selected Text");
        ResourceDictionary inactiveSelectedText = formatMap.GetProperties("Inactive Selected Text");
    
        formatMap.BeginBatchUpdate();
    
        regularCaretProperties[EditorFormatDefinition.ForegroundBrushId] = Brushes.Magenta;
        formatMap.SetProperties("Caret", regularCaretProperties);
    
        overwriteCaretProperties[EditorFormatDefinition.ForegroundBrushId] = Brushes.Turquoise;
        formatMap.SetProperties("Overwrite Caret", overwriteCaretProperties);
    
        indicatorMargin[EditorFormatDefinition.BackgroundColorId] = Colors.LightGreen;
        formatMap.SetProperties("Indicator Margin", indicatorMargin);
    
        visibleWhitespace[EditorFormatDefinition.ForegroundColorId] = Colors.Red;
        formatMap.SetProperties("Visible Whitespace", visibleWhitespace);
    
        selectedText[EditorFormatDefinition.BackgroundBrushId] = Brushes.LightPink;
        formatMap.SetProperties("Selected Text", selectedText);
    
        inactiveSelectedText[EditorFormatDefinition.BackgroundBrushId] = Brushes.DeepPink;
        formatMap.SetProperties("Inactive Selected Text", inactiveSelectedText);
    
        formatMap.EndBatchUpdate();
    }
    

生成并测试代码

生成并测试代码

  1. 生成解决方案。

    当您运行在调试器中查看此项目, Visual Studio 的第二个实例进行实例化。

  2. 创建一个文本文件并键入一些文本。

    • 插入插入符号应是紫红色的,并使用插入符号应是绿松石。

    • 指示器边距 (在文本视图的左边) 应为浅绿色的。

  3. 选择键入的文本。 选定的文本的颜色应是为粉红色的。

  4. 当文本时,任何位置请在文本窗口外单击。 选定的文本的颜色应为指定的 pink。

  5. 打开可见空白。 (在 编辑 菜单上,指向 高级 然后单击 查看空白)。 键入文本中的某些选项。 表示可选的红色箭头应显示。

请参见

概念

编辑扩展点