0
我需要獲取當前文本視圖中ITextSnapshotLine行的分類標記。Visual Studio SDK - IViewTagAggregatorFactoryService.CreateTagAggregator導致異常
首先,我得到了積極的文本視圖:
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;
}
然後,我得到的從視圖ITextViewLine線收集和調用GetClassificationTags每一種方法:
GetClassificationTags(new SnapshotSpan(line.Start, line.Length), textView)
的方法看起來像這個:
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);
}
因此,我有一切正確分類。但是,Visual Studio引發異常並將其記錄到ActivityLog.xml文件中。這種情況只有在第一次對所有行進行分類後纔會發生。在日誌文件中的信息說:
System.InvalidOperationException:
Unexpected false

at Roslyn.Utilities.Contract.ThrowIfFalse(Boolean condition, String message)

at Microsoft.CodeAnalysis.Editor.Implementation.Diagnostics.AbstractDiagnosticsTaggerProvider`1.CreateTagger[T](ITextBuffer buffer)

at Microsoft.CodeAnalysis.Editor.Implementation.Diagnostics.AbstractDiagnosticsTaggerProvider`1.Microsoft.VisualStudio.Text.Tagging.ITaggerProvider.CreateTagger[T](ITextBuffer buffer)

at Microsoft.VisualStudio.Text.Tagging.Implementation.TagAggregator`1.GatherTaggers(ITextBuffer textBuffer)
我注意到異常沒有被註釋掉下面的行之後拋出:
var tagAggregator = viewTagAggregatorFactoryService.CreateTagAggregator<IClassificationTag>(textView);
有時候,也有此異常日誌文件:
System.InvalidOperationException: Unexpected false

at Roslyn.Utilities.Contract.ThrowIfFalse(Boolean condition, String message)

at Microsoft.CodeAnalysis.Editor.Tagging.AbstractAsynchronousTaggerProvider`1.TagSource.GetTagIntervalTreeForBuffer(ITextBuffer buffer)

at Microsoft.CodeAnalysis.Editor.Tagging.AbstractAsynchronousTaggerProvider`1.Tagger.GetTagsWorker(NormalizedSnapshotSpanCollection requestedSpans, Boolean accurate, CancellationToken cancellationToken)

at Microsoft.CodeAnalysis.Editor.Tagging.AbstractAsynchronousTaggerProvider`1.Tagger.GetTags(NormalizedSnapshotSpanCollection requestedSpans)

at Microsoft.VisualStudio.Text.Tagging.Implementation.TagAggregator`1.<GetTagsForBuffer>d__38.MoveNext()
我的問題是:什麼導致這個異常,我該如何擺脫它?
非常感謝。你是對的 - 這段代碼是在後臺線程中執行的。 –