我想重复使用storyboad
中定义的resourcedictionnary
并在 App.xaml
<Storyboard x:Key="ShowWindowStoryboard">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"
>
<EasingDoubleKeyFrame KeyTime="0:0:0.3"
Value="1" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"
>
<EasingDoubleKeyFrame KeyTime="0:0:0.3"
Value="1" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)"
>
<EasingDoubleKeyFrame KeyTime="0:0:0.3"
Value="1" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
这里是给出异常的代码
Storyboard sb = (FindResource("ShowWindowStoryboard") as Storyboard).Clone();
DoubleAnimation da0 = sb.Children[0] as DoubleAnimation;
/* Exception here da0 is null*/
Storyboard.SetTarget(da0, uc);
DoubleAnimation da1 = sb.Children[1] as DoubleAnimation;
Storyboard.SetTarget(da1, uc);
DoubleAnimation da2 = sb.Children[2] as DoubleAnimation;
Storyboard.SetTarget(da2, uc);
sb.Begin();
我还按预期检查了sb.children.Count == 3
。
答案 0 :(得分:2)
您必须将TransformGroup添加到uc
控件的RenderTransform中。我将向图像添加TransformGroup。这同样适用于您的控件uc
。
<强> XAML 强>
<Image Source="untitled.bmp" Name="ImgDemo">
<Image.RenderTransform>
<TransformGroup>
<ScaleTransform/>
</TransformGroup>
</Image.RenderTransform>
</Image>
如果你必须在代码中添加TransformGroup,你可以在调用Story.Begin之前使用以下内容:
TransformGroup transformGroup = new TransformGroup();
transformGroup.Children.Add(new ScaleTransform(1,1));
uc.RenderTransform = transformGroup;
您的Storyboard.TargetProperty
应该反映您希望应用故事板的对象的签名。这就是你面临异常的原因。此外,您可能希望稍微更改Storyboard以实际查看对象的更改。像,
<Storyboard x:Key="ShowWindowStoryboard">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"
>
<EasingDoubleKeyFrame KeyTime="0:0:0.3"
Value="0.5" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"
>
<EasingDoubleKeyFrame KeyTime="0:0:0.3"
Value="0.5" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)"
>
<EasingDoubleKeyFrame KeyTime="0:0:0.3"
Value="0.5" />
</DoubleAnimationUsingKeyFrames>
请注意,值已更改,以便动画显而易见。最后,我确实看到你调用故事板后面的代码没有使用正确的转换。所以你可能想考虑使用以下内容,
Storyboard sb = (FindResource("ShowWindowStoryboard") as Storyboard).Clone();
DoubleAnimationUsingKeyFrames da0 = sb.Children[0] as DoubleAnimationUsingKeyFrames;
Storyboard.SetTarget(da0, ImgDemo);
DoubleAnimationUsingKeyFrames da1 = sb.Children[1] as DoubleAnimationUsingKeyFrames;
Storyboard.SetTarget(da1, ImgDemo);
DoubleAnimationUsingKeyFrames da2 = sb.Children[2] as DoubleAnimationUsingKeyFrames;
Storyboard.SetTarget(da2, ImgDemo);
sb.Begin();
祝你好运!