2

我正在学习 WinRT,我为我的程序定义了一个自定义主题,包括覆盖一些默认颜色

目前我在我的 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 滑块,然后将 S 向下和 B 向上移动,但我想知道我是否可以在 XAML 中执行此操作,所以我必须做的就是改变PrimaryColor 和其他颜色,在此进行相应调整。

4

2 回答 2

6

您可以绑定到静态资源(请参阅是否可以在 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>

左边的矩形是你的颜色,右边的矩形是粉红色的。

于 2013-04-12T19:11:42.050 回答
0

You don't need to use Photoshop to change the colorspaces. Both Visual Studio 2012 and Expression Blend have RGB, HSB, HLS and CYMK colorspace tools.

  • In your Resource dictionary, select the SolidColorBrush.
  • IN the property Grid, click the color item.

enter image description here

  • In the dropdown, select the "Edit Resource".

enter image description here

  • Here's the trick. Click the R, G or B letters (the ones with the underlines) in the Resource dialog. That causes a menu to appear in the Visual studio editor. Pick your new colorspace.

enter image description here

  • Choose another colorspace (HSB in your example). Then use the dialog to change the Saturation or Brightness values.

enter image description here

  • Finally, click the OK button, the color value is modified in the Resource Dictionary.
于 2013-04-12T21:29:42.540 回答