逐步解說: 自訂文字檢視
您可以藉由修改它的撰寫風格對應中的下列屬性的任何自訂文字檢視:
指示區邊界
插入插入號
覆寫插入號
選取的文字
非使用中選取的文字 (也就是選取的文字已遺失焦點)
可見的空白區域
必要條件
若要完成這個逐步解說中,您必須安裝Visual Studio 2010 SDK。 如需有關 Visual Studio 的 SDK,以及要下載的詳細資訊,請參閱Visual Studio 擴充性開發人員中心 MSDN 網站上。
建立受管理的擴充性架構 (MEF) 專案
若要建立 MEF 專案
建立 C# 編輯器類別器的專案或 Visual Basic 編輯器類別器的專案。 為方案命名 ViewPropertyTest。
VSIX 資訊清單編輯器中開啟 source.extension.vsixmanifest 檔案。
請確定Content名包含 MEF 元件的內容類型,以及路徑已設為 ViewPropertyTest.dll。
儲存並關閉 source.extension.vsixmanifest。
刪除現有的類別檔案。
定義內容類型
若要定義內容類型
將類別檔案,並命名為 ViewPropertyModifier。
將下列加入using指示詞 (Imports Visual Basic 中的陳述式)。
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;
宣告類別,名為TestViewCreationListener繼承自IWpfTextViewCreationListener。 匯出類別具有下列屬性:
ContentTypeAttribute若要指定要套用這個接聽程式的內容類型。
TextViewRoleAttribute若要指定這個接聽程式的角色。
<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
這個類別中,在匯入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(); }
建置和測試程式碼
若要建置和測試的程式碼
建置方案。
當您執行此專案在偵錯工具時,會執行個體化 Visual Studio 的第二個執行個體。
建立一個文字檔,並輸入一些文字。
插入插入號應該是洋紅,覆寫插入號應淺粉藍。
指示區邊界 (至左文字檢視中) 應該是光線綠色。
選取您剛才輸入的文字。 所選文字的色彩應為淺粉紅。
選取文字時,按一下文字窗戶外的任意位置。 所選文字的色彩應該深粉紅色。
開啟 [看得見的泛空白字元。 (在編輯 功能表上指向 進階 ,然後按一下 檢視空白區)。 某些索引標籤中輸入的文字。 應該會顯示代表定位點的紅色箭號。