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

问题描述 投票:4回答:1

我正在为Visual Studio中的Properties language构建语法高亮扩展,并且已经构建了分类器/标记器。然而,我坚持为不同的标签设置/选择正确的颜色(即键,值,注释)。

我想让颜色遵循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);
    }
}

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

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

c# visual-studio plugins syntax-highlighting vsix
1个回答
1
投票

基于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

© www.soinside.com 2019 - 2024. All rights reserved.