无法为ContentControl背景颜色设置动画,但可以使用Foreground

Ign*_*cia 4 .net silverlight animation xaml windows-phone-7

我正在尝试为Windows Phone 8中的列表框的选择更改设置动画.以下动画有效:

<ColorAnimation Storyboard.TargetProperty="(ContentControl.Foreground).(SolidColorBrush.Color)" Duration="00:00:00.25" From="{StaticResource PhoneForegroundColor}" To="{StaticResource PhoneAccentColor}" />
Run Code Online (Sandbox Code Playgroud)

但是下一个不起作用(System.InvalidOperationException:无法解析指定对象上的TargetProperty(ContentControl.Background).(SolidColorBrush.Color).

<ColorAnimation Storyboard.TargetProperty="(ContentControl.Background).(SolidColorBrush.Color)" Duration="00:00:00.25" From="{StaticResource PhoneForegroundColor}" To="{StaticResource PhoneAccentColor}" />
Run Code Online (Sandbox Code Playgroud)

对于我的生活,我无法发现原因.

nem*_*esv 6

您需要将Background属性显式设置为您的属性ContentControl才能使动画生效.

你需要这个,因为当动画解析表达式时,属性的默认值Backgroundnull这样的,(ContentControl.Background).(SolidColorBrush.Color)它无法访问该Color属性null.

它的工作原理与 Foreground的默认值Foreground属性是黑色画笔所以它是默认设置为一个有效的非空值.

  • 是的,异常消息在这里很容易引起误解. (2认同)