In my application I'd like to create a fading line that has GradientStops with system colors, I'm trying to do it like this:

<UserControl.Resources>
    <Style x:Key="Divider" TargetType="Rectangle">
        <Setter Property="Height" Value="2" />
        <Setter Property="Fill">
            <Setter.Value>
                <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
                    <GradientStop Color="{StaticResource PhoneChromeBrush}" Offset="0.0" />
                    <GradientStop Color="{StaticResource PhoneInverseBackgroundBrush}" Offset="1.0" />
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>

But when I try to compile project I get the following error:

A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in System.Windows.ni.dll
An exception of type 'System.Windows.Markup.XamlParseException' occurred in System.Windows.ni.dll but was not handled in user code

What should I do to fix this?

有帮助吗?

解决方案

GradientStop.Color expects a color, not a brush. Use PhoneChromeColor and PhoneInverseBackgroundColor instead:

<UserControl.Resources>
    <Style x:Key="Divider" TargetType="Rectangle">
        <Setter Property="Height" Value="2" />
        <Setter Property="Fill">
            <Setter.Value>
                <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
                    <GradientStop Color="{StaticResource PhoneChromeColor}" Offset="0.0" />
                    <GradientStop Color="{StaticResource PhoneInverseBackgroundColor}" Offset="1.0" />
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>

其他提示

I am not certain but it appears you may be setting a static color as a brush. It is hard to tell since you never posted the code to your static resource for 'PhoneChromeBrush' or 'PhoneInverseBackgroundBrush'. But you are setting gradient stops with these and if they are gradients themselves that may break your code. Usually you reserve 'brush' for a gradient so I was not certain:

could you not do something like:

<UserControl.Resources>

<LinearGradientBrush x:Key="MoneyBrush" EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#3A883A" Offset="1" />
                <GradientStop Color="#FFFFFF" Offset="0" />
                <GradientStop Color="#FF53AA75" Offset="0.50" />
                <GradientStop Color="#073307" Offset="0.95" />
            </LinearGradientBrush>
        <Style x:Key="Divider" TargetType="Rectangle">
            <Setter Property="Height" Value="2" />
            <Setter Property="Fill" Value="{StaticResource MoneyBrush}"/>
        </Style>
    </UserControl.Resources>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top