在 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);
}
}
到目前为止我发现了什么:
- This question 假定我的问题已经得到解答。
- This answer 显示了可以在注册表中存储颜色的位置,但这不是可靠的解决方案。
- This question 解决 WPF 的颜色(不是我的情况)
- 有Extensibility Tools extension with the Theme Swatches which show the colors from EnvironmentColors,但是我不知道如何使用它提供的C#代码
如果可能的话,我想使用在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);
}
}
文档:
额外:
这可能有助于选择正确的 ThemeResourceKey
我正在为 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);
}
}
到目前为止我发现了什么:
- This question 假定我的问题已经得到解答。
- This answer 显示了可以在注册表中存储颜色的位置,但这不是可靠的解决方案。
- This question 解决 WPF 的颜色(不是我的情况)
- 有Extensibility Tools extension with the Theme Swatches which show the colors from EnvironmentColors,但是我不知道如何使用它提供的C#代码
如果可能的话,我想使用在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);
}
}
文档:
额外:
这可能有助于选择正确的 ThemeResourceKey