0

我在运行时向主规则集添加了一个新的关键字对象。但除了那些关键字之外,其他规则都正确着色。

谁能解释为什么在运行时加载的单词没有突出显示?

using (Stream stream = typeof(Window1).Assembly.GetManifestResourceStream("testAvalonEdit.MyLang.xshd")) {
    using (XmlReader reader = new XmlTextReader(stream)) {
        xshd = HighlightingLoader.LoadXshd(reader);
        customHighlighting = HighlightingLoader.Load(xshd, HighlightingManager.Instance);
        updateStandardParametersList();
    }
}
HighlightingManager.Instance.RegisterHighlighting("MyLang Highlighting", new string[ { ".s" }, customHighlighting);


其中,突出显示的方法是:

void updateStandardParametersList() {
    //find existing color. It exists for sure.
    XshdColor existingColor = xshd.Elements.OfType<XshdColor>().Where(xc => string.Equals(xc.Name, "StandardParametersColor", StringComparison.CurrentCultureIgnoreCase)).First();

    XshdKeywords newKeyWords = new XshdKeywords();
    newKeyWords.ColorReference = new XshdReference<XshdColor>(existingColor);

    //I add new words to the Keywords object
    for(int i = 1; i < 25; i++)
        newKeyWords.Words.Add("A000" + i.ToString("00"));
    for(int i = 1; i < 25; i++)
        newKeyWords.Words.Add("B000" + i.ToString("00"));
    for(int i = 1; i < 5; i++)
        newKeyWords.Words.Add("C0000" + i);

    XshdRuleSet mainRuleSet = xshd.Elements.OfType<XshdRuleSet>().Where(o => string.IsNullOrEmpty(o.Name)).First();
    mainRuleSet.Elements.Add(newKeyWords);
}

谢谢!


更新 1

在尝试了丹尼尔的建议后,

xshd = HighlightingLoader.LoadXshd(reader);
updateStandardParametersList();
customHighlighting = HighlightingLoader.Load(xshd, HighlightingManager.Instance);

我得到这个例外:

在此处输入图像描述

那么,为什么会抛出这个异常呢?我要做的就是添加Keywords对象并将其颜色设置为 XSHD 中的预定义颜色。

或者,这不是正确的方法吗?

4

1 回答 1

2

该调用根据存储在对象中的信息HighlightingLoader.Load(xshd)创建一个。如果您更改后者,则不会知道这些更改。IHighlightingDefinitionxshdxshdIHighlightingDefinition

要解决此问题,请HighlightingLoader.Load()仅在更新突出显示后调用:

    xshd = HighlightingLoader.LoadXshd(reader);
    updateStandardParametersList();
    customHighlighting = HighlightingLoader.Load(xshd, HighlightingManager.Instance);

对于重复颜色异常:表达式new XshdReference<XshdColor>(existingColor)对应于在关键字元素上内联定义的 XSHD 颜色(它是颜色定义,而不仅仅是参考)。因此,您对颜色有多个定义。

要创建对现有命名颜色的引用,请使用:

    newKeyWords.ColorReference = new XshdReference<XshdColor>(null, "StandardParametersColor");
于 2014-04-23T17:28:26.157 回答