如何在wpf中设置动画路径数据?我们有一系列模板化的silverlight-5控件可以改变这个属性;这些控件需要适应在wpf中工作。在wpf中,当VSM尝试更改状态时,它会崩溃并出现以下异常:
无法使用'System.Windows.Media.Animation.ObjectAnimationUsingKeyFrames'
为'System.Windows.Shapes.Path'设置'Data'属性的动画内部异常: 应用于“数据”属性的动画计算[XXX-The path data-]的当前值,该值不是该属性的有效值。
以下是按钮控件的示例:从圆圈到星星的路径 - 如何在wpf中实现?
<Style x:Key="ButtonStyle1" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="MouseOver">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Path.Data)" Storyboard.TargetName="path">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>M 291.42858 512.36218 216.73569 387.62221 74.321018 416.8994 169.87441 307.31546 98.0216 180.91821 l 133.74814 57.01338 98.00719 -107.39498 -12.89251 144.82014 132.42459 60.0235 -141.71614 32.49039 z</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Viewbox>
<Path x:Name="path" Stroke="Black" Fill="Black" UseLayoutRounding="False" Data="m 357.14285 425.21933 c 0 71.79702 -58.20298 130 -130 130 -71.79701 0 -129.999997 -58.20298 -129.999997 -130 0 -71.79702 58.202987 -130 129.999997 -130 71.79702 0 130 58.20298 130 130 z"/>
</Viewbox>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
答案 0 :(得分:3)
如果不创建对象实例,使用元素语法没有意义,它仍然是一个字符串而Data
是Geometry
,只需将其包装在所述标记中即可。
<DiscreteObjectKeyFrame.Value>
<Geometry>
M 291.42858 512.36218 216.73569 387.62221 74.321018 416.8994 169.87441 307.31546 98.0216 180.91821 l 133.74814 57.01338 98.00719 -107.39498 -12.89251 144.82014 132.42459 60.0235 -141.71614 32.49039 z
</Geometry>
</DiscreteObjectKeyFrame.Value>