在 Visual Studio 分类器扩展中获取主题颜色

Getting theme colors in Visual Studio Classifier extension

我正在为 Visual Studio 中的 Properties language 构建一个语法高亮扩展,并且已经构建了 classifier/tagger。 然而,我被困在 setting/choosing 不同标签(即键、值、注释)的正确颜色。

我想让颜色遵循 Visual Studio 的 当前 主题。 现在很难-编码(参见 ForegroundColor = ...):

[Export(typeof(EditorFormatDefinition))]
[ClassificationType(ClassificationTypeNames = "PropertiesKeyTypeDefinition")]
[Name("PropertiesKeyFormat")]
[Order(Before = Priority.Default)]
internal sealed class PropertiesKey : ClassificationFormatDefinition {
    public PropertiesKey() {
        DisplayName = "Properties Key";
        ForegroundColor = Color.FromRgb(86, 156, 214);
    }
}

到目前为止我发现了什么:

如果可能的话,我想使用在Tools -> Options -> Environment -> Fonts and Colors的VS中可以找到的“Keyword', 'String' and 'Comment”颜色,再次根据当前主题。

根据EnvironmentColors你可以得到一个ThemeResourceKey。

然后可以使用此函数将该键转换为 SolidColorBrush:

private static SolidColorBrush ToBrush(ThemeResourceKey key)
{
    var color = VSColorTheme.GetThemedColor(key);
    return new SolidColorBrush(Color.FromArgb(color.A, color.R, color.G, color.B));
}

因此将它分配给您的前景变为:

[Export(typeof(EditorFormatDefinition))]
[ClassificationType(ClassificationTypeNames = "PropertiesKeyTypeDefinition")]
[Name("PropertiesKeyFormat")]
[Order(Before = Priority.Default)]
internal sealed class PropertiesKey : ClassificationFormatDefinition {
    public PropertiesKey() {
        DisplayName = "Properties Key";
        ForegroundColor = ToBrush(EnvironmentColors.ClassDesignerCommentTextColorKey);
    }
 }

文档:

ThemeResouceKey

VSColorTheme.GetThemedColor

额外:

这可能有助于选择正确的 ThemeResourceKey

VS Colors