【问题标题】:Show tooltip on hover over text将鼠标悬停在文本上时显示工具提示
【发布时间】:2019-08-11 22:02:25
【问题描述】:

我想创建允许在我将鼠标悬停在文本上时显示自定义消息的扩展程序。

例如“test-text”应该给出工具提示“OK”而不是当前的“ITrackin...”

我尝试关注https://docs.microsoft.com/en-us/visualstudio/extensibility/walkthrough-displaying-quickinfo-tooltips?view=vs-2019 但人们说它不起作用,而且要做到这一点还有很长的路要走。

我找不到更多关于此的文档。我知道如何在点击窗口中显示它/获取当前选定的文本。

【问题讨论】:

  • 也许你可以从这个sample得到一些帮助。
  • 谢谢,它似乎在一台机器上工作,但在第二台机器上失败。 (没有错误,两台机器上都是visual studio 2019)
  • 如果它可以在一台机器A上运行而在另一台机器B上不行。也许你可以在A中尝试Export all vs settings,并使用机器B导入它。然后两个VS2019都有相同的设置。之后,确保两个项目都是 C#。
  • 两台机器的设置相同。重装VS2019解决。
  • 是的,我会在今天晚些时候分享。等我有时间来描述它。谢谢提醒。

标签: visual-studio visual-studio-extensions vsix


【解决方案1】:

Lance Li-MSFT 发送的样本确实很有帮助,但为了完成这项工作,我不得不花一些时间。

重要步骤:

  • 导入 LineAsyncQuickInfoSourceProvider.cs 和 LineAsyncQuickInfoSource.cs
  • 通过添加引用对话框添加对 System.ComponentModel.Composition 的引用(右键单击项目名称)
  • 通过使用 NuGet 包管理器安装缺少的引用来获取它们
  • 要初始化 MEF 组件,您需要将新资源添加到 source.extension.vsixmanifest。
<Assets>
    ...
    <Asset Type = "Microsoft.VisualStudio.MefComponent" d:Source="Project" d:ProjectName="%CurrentProject%" Path="|%CurrentProject%|" />
</Assets>

LineAsyncQuickInfoSourceProvider.cs

它只是用来显示快速信息/工具提示。

using Microsoft.VisualStudio.Language.Intellisense;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Utilities;
using System.ComponentModel.Composition;

namespace JSONExtension
{
    [Export(typeof(IAsyncQuickInfoSourceProvider))]
    [Name("Line Async Quick Info Provider")]
    [ContentType("any")]
    [Order]
    internal sealed class LineAsyncQuickInfoSourceProvider : IAsyncQuickInfoSourceProvider
    {
        public IAsyncQuickInfoSource TryCreateQuickInfoSource(ITextBuffer textBuffer) //creates instance of LineAsyncQuickInfoSource for displaying Quick Info
        {  
            return textBuffer.Properties.GetOrCreateSingletonProperty(() => new LineAsyncQuickInfoSource(textBuffer)); //this ensures only one instance per textbuffer is created
        }
    }
}

LineAsyncQuickInfoSource.cs

您可以在此处自定义要显示的内容。

using Microsoft.VisualStudio.Language.Intellisense;
using Microsoft.VisualStudio.Language.StandardClassification;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Adornments;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace JSONExtension
{
    internal sealed class LineAsyncQuickInfoSource : IAsyncQuickInfoSource
    {
        private ITextBuffer _textBuffer;

        public LineAsyncQuickInfoSource(ITextBuffer textBuffer)
        {
            _textBuffer = textBuffer;
        }

        // This is called on a background thread.
        public Task<QuickInfoItem> GetQuickInfoItemAsync(IAsyncQuickInfoSession session, CancellationToken cancellationToken)
        {
            var triggerPoint = session.GetTriggerPoint(_textBuffer.CurrentSnapshot);

            if (triggerPoint != null)
            {
                var line = triggerPoint.Value.GetContainingLine();
                var lineSpan = _textBuffer.CurrentSnapshot.CreateTrackingSpan(line.Extent, SpanTrackingMode.EdgeInclusive);
                var text = triggerPoint.Value.GetContainingLine().GetText(); //get whole line of current cursor pos

                ContainerElement dataElm = new ContainerElement(
                ContainerElementStyle.Stacked,
                new ClassifiedTextElement(
                    new ClassifiedTextRun(PredefinedClassificationTypeNames.Keyword, "MESSAGE TO EDIT: " + text.ToString())
                ));
                return Task.FromResult(new QuickInfoItem(lineSpan, dataElm)); //add custom text from above to Quick Info
            }

            return Task.FromResult<QuickInfoItem>(null); //do not add anything to Quick Info
        }
        public void Dispose()
        {
            // This provider does not perform any cleanup.
        }
    }
}

【讨论】:

    猜你喜欢
    • 2010-10-26
    • 1970-01-01
    • 2018-06-02
    • 1970-01-01
    • 1970-01-01
    • 2011-04-22
    • 2011-09-30
    • 1970-01-01
    相关资源
    最近更新 更多