加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 22:35:07 所属栏目:百科 来源:网络整理
导读:我正在为Visual Studio中的 Properties language构建语法高亮扩展,并且已经构建了分类器/标记器. 然而,我坚持为不同的标签设置/选择正确的颜色(即键,值,注释). 我想让颜色遵循Visual Studio的当前主题.现在它们是硬编码的(参见ForegroundColor = …): [Expo
我正在为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 Tools的Extensibility 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读