2015-06-11 31 views

回答

0

您可以通過編寫自己的Visual Studio風格畫筆並與控件合併以兩種方式進行操作。

另一種方法是,您可以從Windows註冊表中選擇VS主題的資源。在使用Utility類從Windows註冊表中選擇當前主題之前很久。

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版本的resdistributable組件。一旦你通過你的項目添加你可以直接訪問所有畫筆作爲XAML本身的關鍵。例

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

命名空間必須添加作爲

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

+0

ShineKing,這是返回CurrentTheme。你能告訴我如何在Xaml中使用它,例如,如何根據它來設置標籤的顏色。 – Mohsen

+0

Mohsen,我找到了另一種方式將VS直接刷到xaml。當然,它的作品適合你。請看我編輯的代碼 – ReeganLourduraj

+0

Hi ShineKing,我想我仍然失去了一些東西。它仍然不起作用。你介意在哪裏分享你的解決方案。 – Mohsen

相關問題