如何在WPF中使用情节提要 [英] how to use storyboards in WPF

查看:51
本文介绍了如何在WPF中使用情节提要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重用在 resourcedictionnary 中定义并在 App.xaml

I want to reuse a storyboad defined in a resourcedictionnary and referenced in 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 是否符合预期.

i have also checked that sb.children.Count == 3 as expected.

推荐答案

您必须将TransformGroup添加到 uc 控件的RenderTransform.我将向图像添加一个TransformGroup.对于您的控件 uc 同样如此.

You have to add a TransformGroup to the RenderTransform of your uc control. I will be adding a TransformGroup to an Image. The same will work for your control uc.

XAML

<Image Source="untitled.bmp" Name="ImgDemo">
    <Image.RenderTransform>
        <TransformGroup>
            <ScaleTransform/>    
        </TransformGroup>
    </Image.RenderTransform>
</Image>

如果必须在代码后面添加TransformGroup,则可以在调用Story.Begin之前使用以下代码:

If you have to add TransformGroup in code behind, you can use following before calling Story.Begin:

TransformGroup transformGroup = new TransformGroup();
transformGroup.Children.Add(new ScaleTransform(1,1));
uc.RenderTransform = transformGroup;

您的 Storyboard.TargetProperty 应该反映您要应用情节提要的对象的签名.这就是为什么您遇到例外的原因.此外,您可能需要稍微更改情节提要以实际查看对象上的更改.喜欢,

Your Storyboard.TargetProperty should reflect the signature of the object that you want the storyboard to be applied to. This is why you were facing the exception. Also, you might want to change your Storyboard a little to actually see the changes on the object. Like,

<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>

请注意,这些值已更改,因此动画将引人注目.最后,我确实看到您在调用情节提要上的代码没有使用正确的转换.因此,您可能想考虑使用以下内容,

Note that the values are changed so that the animations will be noticeable. Finally, I do see that your code behind on calling the storyboard does not use proper casting. So you might wanna consider using the following,

 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();

祝你好运!

这篇关于如何在WPF中使用情节提要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆