【问题标题】:Developing Visual Studio Extensions getting the current forecolor开发获取当前前景色的 Visual Studio 扩展
【发布时间】:2015-08-26 15:46:30
【问题描述】:

在我的 WPF 用户控件中,如果用户有一个深色主题,我所有默认为黑色的文本都将难以阅读。 VS 当前颜色的正确使用方法是什么。

【问题讨论】:

    标签: wpf visual-studio visual-studio-2013


    【解决方案1】:

    您可以通过两种方式编写自己的 Visual Studio 样式画笔并与控件合并。

    另外,您可以从 Windows 注册表中选择 VS 主题的资源。很久以前我使用 Utility 类从 windows Registry 中选择当前主题。

    public enum VsTheme
    {
        Unknown = 0, 
        Light, 
        Dark, 
        Blue
    }
    
    public class ThemeUtil
    {
        private static readonly IDictionary<string, VsTheme> Themes = new Dictionary<string, VsTheme>()
        {
            { "de3dbbcd-f642-433c-8353-8f1df4370aba", VsTheme.Light }, 
            { "1ded0138-47ce-435e-84ef-9ec1f439b749", VsTheme.Dark }, 
            { "a4d6a176-b948-4b29-8c66-53c97a1ed7d0", VsTheme.Blue }
        };
    
        public static VsTheme GetCurrentTheme()
        {
            string themeId = GetThemeId();
            if (string.IsNullOrWhiteSpace(themeId) == false)
            {
                VsTheme theme;
                if (Themes.TryGetValue(themeId, out theme))
                {
                    return theme;
                }
            }
    
            return VsTheme.Unknown;
        }
    
        public static string GetThemeId()
        {
            const string CategoryName = "General";
            const string ThemePropertyName = "CurrentTheme";
            string keyName = string.Format(@"Software\Microsoft\VisualStudio\11.0\{0}", CategoryName);
    
            using (RegistryKey key = Registry.CurrentUser.OpenSubKey(keyName))
            {
                if (key != null)
                {
                    return (string)key.GetValue(ThemePropertyName, string.Empty);
                }
            }
    
            return null;
        }
    }
    

    我找到了另一种方法。我可以直接使用来自 xaml 的 Visual Studio 主题颜色资源。对于这些,您需要 Microsoft.VisualStudio.Shell.12.0.(适用于 VS2013) 它是 VisualStudio 版本的可再分发组件。通过项目添加后,您可以直接访问所有画笔作为 XAML 本身的键。例如

    Background="{DynamicResource {x:Static vsfx:VsBrushes.EnvironmentBackgroundGradientKey}}"
    

    NameSpace 必须添加为

    xmlns:vsfx="clr-namespace:Microsoft.VisualStudio.Shell;assembly=Microsoft.VisualStudio.Shell.12.0"
    

    您可以参考以下 MSDN 链接中的所有画笔

    https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.shell.vsbrushes(v=vs.120).aspx

    如果要检测主题更改事件本身,可以使用 VSColorTheme.ThemeChanged 事件

    https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.platformui.vscolortheme.themechanged.aspx

    【讨论】:

    • ShineKing,这是返回 CurrentTheme。你能告诉我如何在 Xaml 中使用它,例如如何根据它设置标签的颜色。
    • Mohsen,我找到了另一种将 VS 的画笔直接引用到 xaml 的方法。当然它对你有用。请看我编辑的代码
    • 嗨 ShineKing,我想我仍然缺少一些东西。它仍然无法正常工作。您介意在某处分享您的解决方案吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-13
    • 1970-01-01
    • 2012-02-20
    • 1970-01-01
    相关资源
    最近更新 更多