如何创建AvalonEdit语法文件(.xshd)并将其嵌入到程序集中? [英] How do I create an AvalonEdit syntax file (.xshd) and embed it into my assembly?

查看:508
本文介绍了如何创建AvalonEdit语法文件(.xshd)并将其嵌入到程序集中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为AvalonEdit定义一些自定义突出显示规则.似乎没有任何文档,有有关文件格式的一些文档,但是创建后如何实际加载和使用该定义则没有任何意义.

I'm trying to define some custom highlighting rules for AvalonEdit. There doesn't seem to be any documentation for this -- there's some documentation on the file format, but nothing on how to actually load and use the definition once you've created it.

"突出显示语法"维基页面(用于旧的WinForms TextEditor)说明了如何可以从磁盘上的.xshd文件加载突出显示的定义,但我宁愿将其嵌入为资源,就像AvalonEdit使用其内置定义一样.

The "Syntax highlighting" wiki page (for the old WinForms TextEditor) documents how to load highlighting definitions from a .xshd file on disk, but I'd rather embed it as a resource, the same way AvalonEdit does with its built-in definitions.

我已经查看了ICSharpCode.AvalonEdit项目中的代码,该项目加载了其内置的荧光笔,但是实际的从资源中加载"代码是在DefaultHighlightingManager.LoadHighlighting中完成的,该代码是私有的,并且要进行创建怪异的东西,显然只在发布版本中被称为.

I've looked at the code in the ICSharpCode.AvalonEdit project that loads its built-in highlighters, but the actual "load from resource" code is done in DefaultHighlightingManager.LoadHighlighting, which is private -- and which, to make things weirder, is apparently only called in release builds.

我可以不断摸索,从私有方法中复制/粘贴一些代码,并尝试将可能有用或不可行的东西拼凑在一起,但是似乎值得问:什么是标准方法这样做吗?是否有建议的第三方代码从资源中加载突出显示定义的方法?

I can keep fumbling around, copy/paste some of the code from the private methods, and try to cobble something together that might or might not work, but it seems worthwhile to ask: what is the standard way to do this? Is there a recommended way for third-party code to load a highlighting definition from a resource?

Daniel's answer gave me a good start, but you need to include the namespace name in the string you pass to GetManifestResourceStream -- if you don't, you'll get a NullReferenceException (because it returns a null stream).

我最终使用以下方法将名为ResourceLoader的静态类添加到与.xshd文件相同的项目文件夹中:

I wound up adding a static class called ResourceLoader to the same project folder as the .xshd file, with this method:

public static IHighlightingDefinition LoadHighlightingDefinition(
    string resourceName)
{
    var type = typeof(ResourceLoader);
    var fullName = type.Namespace + "." + resourceName;
    using (var stream = type.Assembly.GetManifestResourceStream(fullName))
    using (var reader = new XmlTextReader(stream))
        return HighlightingLoader.Load(reader, HighlightingManager.Instance);
}

然后我可以打电话给ResourceLoader.LoadHighlightingDefinition("Name.xshd").

对于在家中跟随的任何人,.xshd文件都需要将其构建操作"设置为嵌入式资源".

For anyone following along at home, the .xshd file needs to have its Build Action set to Embedded Resource.

推荐答案

您可以使用类ICSharpCode.AvalonEdit.Highlighting.Xshd.HighlightingLoader中的静态方法来加载.xshd文件. 例如:

You can use the static methods in the class ICSharpCode.AvalonEdit.Highlighting.Xshd.HighlightingLoader to load .xshd files. For example:

using (Stream s = myAssembly.GetManifestResourceStream("MyHighlighting.xshd")) {
    using (XmlTextReader reader = new XmlTextReader(s)) {
        textEditor.SyntaxHighlighting = HighlightingLoader.Load(reader, HighlightingManager.Instance);
    }
}

AvalonEdit本身的加载代码很奇怪,因为它急切地将xshds加载到调试版本中(以便立即注意到它们中的错误),但将它们延迟加载到发行版本中.

The loading code in AvalonEdit itself is weird because it eagerly loads the xshds in debug builds (so that errors in them are noticed immediately), but delay-loads them in release builds.

这篇关于如何创建AvalonEdit语法文件(.xshd)并将其嵌入到程序集中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆