我正在研究这个例子并发现了问题:调用方法后语法突出显示不适用于 C# 文件
_BufferAdapter.GetLanguageServiceID("694dd9b6-b865-4c5b-ad85-86356e9c88dc")
此问题在 VS 2015 中重现,在 VS 2013 中不重现。对于 VB 文件,我有同样的问题。我认为问题与 VS 2015 中用于 C#/VB 编辑器的新 Roslyn 引擎有关。如何为 C# 和 VB 文件启用语法突出显示?通常,我想创建一个简单的 VS 编辑器并将其 WPF 内容托管到我的 WPF 控件中。
我的代码中的InitializeEditor方法:
private void InitializeEditor(string path)
{
var text = File.ReadAllText(path); // path to the .cs file
var componentModel = (IComponentModel)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SComponentModel));
_OleServiceProvider = (IOleServiceProvider)GetService(typeof(IOleServiceProvider));
_BufferFactory = componentModel.GetService<ITextBufferFactoryService>();
var contentType = componentModel.GetService<IContentTypeRegistryService>().GetContentType("CSharp");
_EditorAdapterFactory = componentModel.GetService<IVsEditorAdaptersFactoryService>();
_BufferAdapter = _EditorAdapterFactory.CreateVsTextBufferAdapter(_OleServiceProvider, contentType);
_BufferAdapter.InitializeContent(text, text.Length);
_BufferAdapter.SetLanguageServiceID("694dd9b6-b865-4c5b-ad85-86356e9c88dc")
_ViewAdapter = _EditorAdapterFactory.CreateVsTextViewAdapter(_OleServiceProvider);
((IVsWindowPane)_ViewAdapter).SetSite(_OleServiceProvider);
var initView = new[] { new INITVIEW() };
initView[0].fSelectionMargin = 0;
initView[0].fWidgetMargin = 0;
initView[0].fVirtualSpace = 0;
initView[0].fDragDropMove = 1;
_ViewAdapter.Initialize(_BufferAdapter as IVsTextLines, IntPtr.Zero,
(uint)TextViewInitFlags.VIF_HSCROLL |
(uint)TextViewInitFlags.VIF_VSCROLL |
(uint)TextViewInitFlags3.VIF_NO_HWND_SUPPORT, initView);
}
我在内容类型“CSharp”上替换了 _BufferFactory.TextContentType。我希望它能解决问题。但是新的内容类型并没有解决突出显示的问题。