gpt4 book ai didi

c# - 从 Visual Studio 文本装饰扩展中获取当前文件名

转载 作者:太空狗 更新时间:2023-10-29 21:43:51 27 4
gpt4 key购买 nike

我是 VS 扩展开发的新手。我目前正在使用 VS 2015 中的文本装饰示例,并且能够正确显示彩色框。现在我想扩展示例,使装饰只出现在某些文件名上。

谷歌搜索说我可以使用 ITextDocumentFactoryService.TryGetTextDocument 接口(interface)和 IWpfTextView.TextBuffer 属性来获取文件名。这听起来不错。但我似乎无法实际获取界面。

在我的课上我有:

    [Import]
public ITextDocumentFactoryService TextDocumentFactoryService = null;

但它始终为 NULL。

如何获取 ITextDocumentFactoryService

namespace Test
{
internal sealed class TestAdornment
{
[Import]
public ITextDocumentFactoryService TextDocumentFactoryService = null;

public TestAdornment(IWpfTextView view)
{
}

/// <summary>
/// Adds the scarlet box behind the 'a' characters within the given line
/// </summary>
/// <param name="line">Line to add the adornments</param>
private void CreateVisuals(ITextViewLine line)
{
// TextDocumentFactoryService is NULL
}
}
}

最佳答案

TextAdornmentTextViewCreationListener.cs

[Export(typeof(IWpfTextViewCreationListener))]
[ContentType("text")]
[TextViewRole(PredefinedTextViewRoles.Document)]
internal sealed class TextAdornmentTextViewCreationListener : IWpfTextViewCreationListener
{
[Import]
public ITextDocumentFactoryService textDocumentFactory { get; set; }

//...

public void TextViewCreated(IWpfTextView textView)
{
new TextAdornment(textView, textDocumentFactory);
}
}

TextAdornment.cs

internal sealed class TextAdornment
{
private readonly ITextDocumentFactoryService textDocumentFactory;
private ITextDocument TextDocument;

//...

public TextAdornment(IWpfTextView view, ITextDocumentFactoryService textDocumentFactory)
{
//...

this.textDocumentFactory = textDocumentFactory;

//...
}

internal void OnLayoutChanged(object sender, TextViewLayoutChangedEventArgs e)
{
var res = this.textDocumentFactory.TryGetTextDocument(this.view.TextBuffer, out this.TextDocument);
if (res)
{
//this.TextDocument.FilePath;
}
else
{
//ERROR
}
}
}

关于c# - 从 Visual Studio 文本装饰扩展中获取当前文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33915010/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com