如何将当前 VS 主题应用于现有控件

How to apply current VS theme to existing controls

我正在开发的应用程序有桌面版和 Visual Studio 扩展版,两者大致提供相同的功能,因此重用了同一组 WPF 控件。这些控件位于 WPF 控件库中。一切都按预期工作。

最近我发现,如果我将 Visual Studio 切换到深色主题,扩展看起来不太好。我用谷歌搜索的解决方案是使用 Visual Studio 的内置画笔和颜色。

我该怎么做?我的控件具有深度嵌套的样式和颜色。如果我在 VS 扩展中创建其他样式,WPF 仍将应用本地画笔和在控件内定义的值。如何解决这个问题?

您可以像这样使用 Visual Studio 的画笔和颜色:

<UserControl ... 
    xmlns:vsShell="clr-namespace:Microsoft.VisualStudio.PlatformUI;assembly=Microsoft.VisualStudio.Shell.14.0">

<TextBox Foreground="{DynamicResource {x:Static vsShell:EnvironmentColors.ToolWindowTextBrushKey}}" />

您还可以使用在所有 VS 主题(如 AccentBorderColorKey)中具有不同颜色的 ThemeResourceKey 检查哪个 VS 主题处于活动状态,并检查返回哪种颜色。

var color = VSColorTheme.GetThemedColor(EnvironmentColors.AccentBorderColorKey);

并为您的 WPF 控件分配您自己的(更好看的)颜色。

文档:

ThemeResouceKey

VSColorTheme.GetThemedColor

额外:

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

VS Colors