微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我正在为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);
    }
}

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

> This question假设我的问题已经回答了.
> This answer显示注册表中可以存储颜色的位置,但这不是一个可靠的解决方案.
> This question解决WPF的颜色(不是我的情况)
> Extensibility ToolsExtensibility Tools扩展显示EnvironmentColors的颜色,但我不知道如何使用它提供的C#代码

如果可能的话,我想使用用于“Keyword”,“String”和“Comment”颜色的颜色,这些颜色可以在工具中的VS中找到 – >选项 – >环境 – >字体和颜色再次符合当前主题.

解决方法

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐