【问题标题】:Get the current filename from a Visual Studio text adornment extension从 Visual Studio 文本装饰扩展中获取当前文件名
【发布时间】:2015-11-25 11:11:50
【问题描述】:

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

谷歌搜索说我可以使用ITextDocumentFactoryService.TryGetTextDocument 接口和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
        }
    }
}

【问题讨论】:

    标签: c# mef


    【解决方案1】:

    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
                }
            }
        }
    

    【讨论】:

    • 你应该添加一些解释。
    • 谢谢!它对我有用。有没有办法获取相对于项目根目录的路径(解决方案)?
    【解决方案2】:

    你是通过依赖注入得到的。

    由于您只提交了 2 行代码,我想您的上下文是由您明确设置的,或者由调用您代码的某个环境隐式设置。

    • 您应该声明属性而不是 字段
    • 应该是公开的

    那么大哥会在你第一次访问之前自动为你设置好。

    ...或...

    您可以改用构造函数注入。注意:创建课程的人不是您。

    private readonly ITextDocumentFactoryService _textDocumentFactoryService;
    
    [ImportingConstructor]
    internal YourClass(ITextDocumentFactoryService textDocumentFactoryService)
    {
        _textDocumentFactoryService = textDocumentFactoryService;
    }
    

    【讨论】:

    • 看起来它已正确声明,但未设置。我的成员调用 (CreateVisuals) 声明它为空。因此,我的上下文不能设置正确。
    【解决方案3】:

    所以在我的情况下,我需要将导入语句放入 AdornmentTextViewCreationListener。这实现了 IWpfTextViewCreationListener 并且是具有以下装饰类的类。

    [Export(typeof(IWpfTextViewCreationListener))]
    [ContentType("text")]
    [TextViewRole(PredefinedTextViewRoles.Document)]
    

    那我可以加了

    private readonly ITextDocumentFactoryService _textDocumentFactoryService;
    [ImportingConstructor]
    

    到我的班级

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-26
      • 2022-08-02
      • 1970-01-01
      • 2010-11-23
      • 1970-01-01
      相关资源
      最近更新 更多