共用方式為


使用新的邊界擴充Visual Studio編輯器

文字檢視邊界會放在邊界容器中(請參閱 ContainerMarginPlacement.KnownValues),並在相對於其他邊界之前或之後排序 (請參閱 MarginPlacement.KnownValues)。

文字檢視邊界提供者透過實作 ITextViewMarginProvider 介面,以及 TextViewMarginProviderConfiguration 來配置他們提供的邊界,並在啟用後,透過 CreateVisualElementAsync 提供將 UI 控制項裝載於邊界中的功能。

由於 VisualStudio.Extensibility 中的延伸模組可能來自 Visual Studio 的跨進程,因此無法直接使用 WPF 做為文字檢視邊界內容的呈現層。 相反地,為文字檢視邊界提供內容需要建立 RemoteUserControl,以及該控件的對應數據範本。 雖然下面有一些簡單的範例,但建議您在建立文字檢視邊距 UI 內容時,閱讀 遠端 UI 文件

/// <summary>
/// Configures the margin to be placed to the left of built-in Visual Studio line number margin.
/// </summary>
public TextViewMarginProviderConfiguration TextViewMarginProviderConfiguration => new(marginContainer: ContainerMarginPlacement.KnownValues.BottomRightCorner)
{
    Before = new[] { MarginPlacement.KnownValues.RowMargin },
};

/// <summary>
/// Creates a remotable visual element representing the content of the margin.
/// </summary>
public async Task<IRemoteUserControl> CreateVisualElementAsync(ITextViewSnapshot textView, CancellationToken cancellationToken)
{
    var documentSnapshot = await textView.GetTextDocumentAsync(cancellationToken);
    var dataModel = new WordCountData();
    dataModel.WordCount = CountWords(documentSnapshot);
    this.dataModels[textView.Uri] = dataModel;
    return new MyMarginContent(dataModel);
}

除了設定邊距位置之外,文字檢視邊距提供者也可以使用 GridCellLengthGridUnitType 屬性來設定放置邊距的網格單元格大小。

文字檢視邊界通常會將與文字檢視相關的某些數據可視化(例如,目前的行號或錯誤計數),因此大部分的文字檢視邊界提供者也會想要 接聽文字檢視事件, 回應文字檢視的開啟、關閉和用戶輸入。

Visual Studio 只會建立文字檢視邊界提供者的一個實例,而不論用戶開啟多少個適用的文字檢視,因此如果您的邊界顯示一些具狀態的數據,您的提供者必須保留目前開啟的文字檢視的狀態。

如需詳細資訊,請參閱 字數計數邊界範例

尚不支援其內容必須與文字檢視行對齊的垂直文字檢視邊界。