如何解冻/防止冻结具有样式的动态创建对象的属性
本文关键字:样式 动态 属性 创建对象 冻结 何解冻 解冻 | 更新日期: 2023-09-27 18:34:34
在我的 XAML 中,我得到了一个用于附加到动态创建的Path
对象的Style
:
<Style x:Key="styleBladePiece" TargetType="{x:Type Path}">
<Setter Property="Data" Value="..." />
<Setter Property="Fill">
<Setter.Value>
<SolidColorBrush Color="{StaticResource BladeFillColor1}" />
</Setter.Value>
</Setter>
</Style>
在我的代码中,我创建了很多这样的代码:
Path pathBladePiece = new Path();
pathBladePiece.Style = (Style)this.FindResource("styleBladePiece");
现在,在后面的步骤中,我尝试对Path
对象的 Fill
属性的Color
进行动画处理,如下所示:
ColorAnimation a = new ColorAnimation();
a.Duration = new Duration(new TimeSpan(0, 0, 0, 0, 400));
a.To = (Color)this.FindResource("BladeFillColor2");
pathBladePiece.Fill.BeginAnimation(SolidColorBrush.ColorProperty, a);
但事实证明,Fill
财产被冻结到那个时候。在设计器和运行时,我得到:InvalidOperationException: property "Color" on "System.Windows.Media.SolidColorBrush" can not be animated, because the object is sealed or locked.
调试 XDecProc.exe 时:A first chance exception of type 'Microsoft.Expression.Markup.DocumentModel.StatePersistingException' occurred in Microsoft.Expression.DesignSurface.dll" when debugging the designer.
我可以解冻它或防止它被冻结吗?
尝试使用 Shared="False"
,来自 MSDN
x:Shared="false" 的另一种情况是,如果对动画值使用可冻结资源,但希望基于每个动画修改资源
将SolidBrush
定义为资源
<SolidColorBrush x:Key="BladeFillBrush1" x:Shared="False" Color="{StaticResource BladeFillColor1}"/>
然后
<Style x:Key="styleBladePiece" TargetType="{x:Type Path}">
<Setter Property="Fill" Value="{StaticResource BladeFillBrush1}">
</Style>