2013-04-12 46 views
1

我學習WinRT中定義的顏色,我定義了一個自定義主題我的程序,包括覆蓋某些默認的顏色XAML基礎上另一種顏色

目前,我做這樣的事情在我的App.xaml

<Application> 
    <Application.Resource> 
     <ResourceDictionary> 
      ... 
      <Color x:Key="PrimaryColor">#FF0055A3</Color> 
      <Color x:Key="PrimaryColorHighlighShade">#FF1263B0</Color> 
      <Color x:Key="PrimaryColorClickShade">#FF2674BD</Color> 
      ... 
      <SolidColorBrush x:Key="SliderTrackDecreaseBackgroundThemeBrush" Color="{StaticResoruce PrimaryColor}" /> 
      <SolidColorBrush x:Key="SliderTrackDecreasePointerOverBackgroundThemeBrush" Color="{StaticResoruce PrimaryColorHighlighShade}" /> 
      <SolidColorBrush x:Key="SliderTrackDecreasePressedBackgroundThemeBrush" Color="{StaticResoruce PrimaryColorClickShade}" /> 
      ... 
     </ResourceDictionary> 
    </Application.Resource> 

,以獲得高光色調和ClickShade,我打開Photoshop中,後藤的HSB滑塊,然後移動羽絨與B,但是,我想知道如果我能做到這一點的XAML使所有我必須做什麼改變PrimaryColor,和其他顏色,其他e相應調整。

回答

5

您可以綁定到靜態資源(請參閱Is it possible to supply a type converter for a static resource in WPF?),並使用值轉換器基於您提供的顏色構建新的顏色。

編輯:

下面是一些代碼來解釋:

值變換器代碼(爲簡單起見,我永遠只是添加紅色,你可以做更復雜的計算,只要你喜歡):

class ColorConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     if (value is Color) 
     { 
      var theColor = Color.Add((Color)value, Color.FromArgb(255,255,0,0)); 
      return theColor; 
     } 
     else 
     { 
      return null; 
     } 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

我的App.xaml看起來是這樣的:

<Application x:Class="SO_15979100.App" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:local="clr-namespace:SO_15979100" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      StartupUri="MainWindow.xaml"> 
    <Application.Resources> 
     <Color x:Key="PrimaryColor">#FF0055A3</Color> 
     <local:ColorConverter x:Key="MyConverter" /> 
     <SolidColorBrush x:Key="PrimaryColorBrush" Color="{StaticResource PrimaryColor}" /> 
     <SolidColorBrush x:Key="ConvertedPrimaryColorBrush" Color="{Binding Source={StaticResource PrimaryColor}, Converter={StaticResource MyConverter}}" /> 
    </Application.Resources> 
</Application> 

請注意,我已經包含了一個本地命名空間來讓轉換器在手邊。

我的主窗口的定義是這樣的:

<Window x:Class="SO_15979100.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition /> 
      <ColumnDefinition /> 
     </Grid.ColumnDefinitions> 

     <Rectangle Grid.Column="0" Fill="{StaticResource PrimaryColorBrush}" /> 
     <Rectangle Grid.Column="1" Fill="{StaticResource ConvertedPrimaryColorBrush}" /> 
    </Grid> 
</Window> 

左邊的矩形是你的顏色,右邊的一個是粉紅色的。

0

您不需要使用Photoshop來更改顏色空間。 Visual Studio 2012和Expression Blend都具有RGB,HSB,HLS和CYMK色彩空間工具。

  • 在您的資源字典中,選擇SolidColorBrush。
  • 在屬性Grid中,單擊顏色項目。

enter image description here

  • 在下拉列表中,選擇 「編輯資源」。

enter image description here

  • 這裏的竅門。在「資源」對話框中單擊R,G或B字母(帶下劃線的字母)。這會導致菜單出現在Visual Studio編輯器中。選擇你的新顏色空間。

enter image description here

  • 選擇另一種色彩空間(HSB在你的例子)。然後使用對話框更改飽和度或亮度值。

enter image description here

  • 最後,單擊OK按鈕,顏色值是在資源字典修改。
+0

您仍然需要通過設計師編輯顏色。如果你想自動改變HSB,那麼你需要編寫一些代碼。 –

相關問題