我已经编写了一个控件,并成功地创建了一个故事板,以便在触发事件期间生成动画。它会在一段时间内改变椭圆的填充。我没有在每次需要更改填充时编写一个新的RadialGradientBrush,而是在资源中提供了其中的两个。
编辑:我有一个Ellipse,它是控件的主要组件,受动画的影响。它的实现很简单,如下所示:
<Ellipse Name="myEllipse" Style="{StaticResource DimStyle}" />当我将它添加到故事板中时(而不是将画笔作为资源引用),我的动画工作就像预期的那样。当我将画笔作为资源引用时,我得到了以下异常:
"Cannot find resource named 'IlluminatedStyle'. Resource names are case sensitive."
在故事板内部,这是当前引用它的地方:
<UserControl.Resources>
<Storyboard x:Key="Foo">
<ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames.KeyFrames>
<DiscreteObjectKeyFrame KeyTime="0:0:0.01" Value="{StaticResource IlluminatedStyle}" />
<DiscreteObjectKeyFrame KeyTime="0:0:0.85" Value="{StaticResource DimStyle}" />
</ObjectAnimationUsingKeyFrames.KeyFrames>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</UserControl.Resources>样式非常相同,只有GradientStop颜色属性不同,所以我只为一个示例提供一种样式。
样式引用的
<UserControl.Resources>
<Style x:Key="IlluminatedStyle" TargetType="Ellipse">
<Setter Property="Fill">
<Setter.Value>
<RadialGradientBrush>
<GradientStop Color="#FF215416" Offset="1"/>
<GradientStop Color="#FE38DA2E" Offset="0"/>
<GradientStop Color="#FE81FF79" Offset="0.688"/>
</RadialGradientBrush>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>那么,如何在Storyboard中正确引用这样的样式呢?
注意事项:Storyboard和Style都包含在相同的UserControl.Resources标记中,但在本例中却被分离出来。
编辑
我将Style放在UserControl.Resources中的Storyboard之前,现在我得到了一个异常,声明:
"This Freezable cannot be frozen.
at System.Windows.Freezable.Freeze()
at System.Windows.Freezable.GetCurrentValueAsFrozen()
at System.Windows.Media.Animation.TimelineCollection.GetCurrentValueAsFrozenCore(Freezable source)
at System.Windows.Freezable.CloneCoreCommon(Freezable sourceFreezable, Boolean useCurrentValue, Boolean cloneFrozenValues)
at System.Windows.Media.Animation.Timeline.GetCurrentValueAsFrozenCore(Freezable sourceFreezable)
at System.Windows.Freezable.GetCurrentValueAsFrozen()
at System.Windows.Media.Animation.Clock..ctor(Timeline timeline)
at System.Windows.Media.Animation.TimelineGroup.AllocateClock()
at System.Windows.Media.Animation.Clock.AllocateClock(Timeline timeline, Boolean hasControllableRoot)"发布于 2010-12-02 16:06:56
由于我对WPF和XAML并不熟悉,所以我犯了一个错误,就是把我的资源变成了一种样式,而没有意识到我本可以简单地将画笔作为资源,而完全避免使用样式。
我将对DiscreteObjectKeyFrame值的引用保留为对新画笔资源的静态引用。我把椭圆换成了这个:
<Ellipse Name="myEllipse" Fill="{StaticResource DimBrush" />样式属性被移除,我直接将画笔分配给fill属性。在ObjectAnimationUsingKeyFrames标记中,我添加了Fill作为Storyboard.TargetProperty,因为我不再使用样式来修饰填充。DiscreteObjectKeyFrame现在看起来如下所示:
<DiscreteObjectKeyFrame KeyTime="0:0:0.01" Value="{StaticResource IlluminatedBrush}" />
<DiscreteObjectKeyFrame KeyTime="0:0:0.85" Value="{StaticResource DimBrush}" />我的资源要简单得多,不用用一种风格包装,而IMO则更优雅。另外,画笔是在我的最终解决方案中的动画之前定义的。
<UserControl.Resources>
<RadialGradientBrush x:Key="DimBrush" >
<GradientStop Color="#FF21471A" Offset="1"/>
<GradientStop Color="#FF33802F" Offset="0"/>
<GradientStop Color="#FF35932F" Offset="0.688"/>
</RadialGradientBrush>
<RadialGradientBrush x:Key="IlluminatedBrush">
<GradientStop Color="#FF215416" Offset="1"/>
<GradientStop Color="#FE38DA2E" Offset="0"/>
<GradientStop Color="#FE81FF79" Offset="0.688"/>
</RadialGradientBrush>
<!-- Storyboard code follows... -->
</UserControl.Resources>现在一切都如愿以偿。我所能做的最好的假设是,样式是不可冻结的,因为它们是我删除的组件,而且我不再收到关于无法冻结的可冻结组件的异常。
发布于 2010-12-02 14:38:27
不能冻结Freezable有三个原因:
。
所以,首先找出是哪个Freezable引起了麻烦,然后检查上面的内容。
https://stackoverflow.com/questions/4335855
复制相似问题