Visual Studio SDK - IViewTagAggregatorFactoryService.CreateTagAggregator导致异常

时间:2016-01-30 17:55:23

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

我需要在当前文本视图中获取ITextSnapshotLine行的分类标记。

首先,我获得了有效的文本视图:

class DPolygon
{
    //Your other attributes here..
    private double[] x,y,z;
    public DPolygon(){
        initCoordinates();    //Init all arrays in the constructor 
    }

    //Initialize all x,y,z arrays.
    private void initCoordinates(){
        x = new double[]{0.0, 0.0, 0.0, 0.0};
        y = new double[]{0.0, 0.0, 0.0, 0.0};
        z = new double[]{0.0, 0.0, 0.0, 0.0};
    }
}

然后,我从视图中获取ITextViewLine行的集合,并在每个上调用GetClassificationTags方法:

public static IWpfTextView GetTextView()
{
    var textManager = (IVsTextManager)ServiceProvider.GlobalProvider.GetService(typeof(SVsTextManager));
    IVsTextView vTextView = null;
    var mustHaveFocus = 1;
    textManager.GetActiveView(mustHaveFocus, null, out vTextView);
    var userData = vTextView as IVsUserData;
    if (userData != null)
    {
        IWpfTextViewHost viewHost;
        object holder;
        var guidViewHost = DefGuidList.guidIWpfTextViewHost;
        userData.GetData(ref guidViewHost, out holder);
        viewHost = (IWpfTextViewHost)holder;
        var textView = viewHost.TextView;
        return textView;
    }

    return null;
}

方法如下:

GetClassificationTags(new SnapshotSpan(line.Start, line.Length), textView)

因此,我将所有内容都归类为正确。但是,Visual Studio会抛出异常并将其记录到ActivityLog.xml文件中。只有在第一次对所有行进行分类后才会发生这种情况。日志文件中的信息表示:

public IEnumerable<IMappingTagSpan<IClassificationTag>> GetClassificationTags(SnapshotSpan span, ITextView textView)
{
    var snapshot = textView.TextSnapshot;

    var componentModel = (IComponentModel)ServiceProvider.GlobalProvider.GetService(typeof(SComponentModel));
    var exportProvider = componentModel.DefaultExportProvider;

    var viewTagAggregatorFactoryService = exportProvider.GetExportedValue<IViewTagAggregatorFactoryService>();

    var tagAggregator = viewTagAggregatorFactoryService.CreateTagAggregator<IClassificationTag>(textView);

    return tagAggregator.GetTags(span);
}

我注意到在注释掉下面的行之后没有抛出异常:

System.InvalidOperationException:
Unexpected false&#x000D;&#x000A;
at Roslyn.Utilities.Contract.ThrowIfFalse(Boolean condition, String message)&#x000D;&#x000A;
at Microsoft.CodeAnalysis.Editor.Implementation.Diagnostics.AbstractDiagnosticsTaggerProvider`1.CreateTagger[T](ITextBuffer buffer)&#x000D;&#x000A;
at Microsoft.CodeAnalysis.Editor.Implementation.Diagnostics.AbstractDiagnosticsTaggerProvider`1.Microsoft.VisualStudio.Text.Tagging.ITaggerProvider.CreateTagger[T](ITextBuffer buffer)&#x000D;&#x000A;
at Microsoft.VisualStudio.Text.Tagging.Implementation.TagAggregator`1.GatherTaggers(ITextBuffer textBuffer)

有时,日志文件中也存在此异常:

var tagAggregator = viewTagAggregatorFactoryService.CreateTagAggregator<IClassificationTag>(textView);

我的问题是:导致此异常的原因是什么,我该如何摆脱它?

1 个答案:

答案 0 :(得分:1)

The code is open source,你可以看看。我的猜测是你在后台线程上尝试这个。