silverlight   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了silverlight中用代码动态控制Storyboard(动画)属性的几种方法大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

准备一个基本的StoryBoard的xaml代码页面 <navigation:Page            xmlns="http://scheR_504_11845@as.microsoft.com/winfx/2006/xaml/presentation"            xmlns:x="http://scheR_504_11845@as.microsoft.com/winfx/2006/xaml"            xm

准备一个基本的StoryBoard的xaml代码页面

<navigation:Page
           xmlns="http://scheR_504_11845@as.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://scheR_504_11845@as.microsoft.com/winfx/2006/xaml"
           xmlns:d="http://scheR_504_11845@as.microsoft.com/expression/blend/2008"
           xmlns:mc="http://scheR_504_11845@as.openxmlformats.org/markup-compatibility/2006"
           mc:Ignorable="d"
           xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
           x:Class="Projection.Test.TestPage"
           d:DesignWidth="640" d:DesignHeight="480"
           title="TestPage Page">
    <navigation:Page.resources>
       
        <Storyboard x:Name="storyTest">
           
            <!--绕y轴旋转,从0到360,再转到720度-->
            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="img" Storyboard.TargetProperty="(UIElement.projection).(PlaneProjection.RotationY)">
                <EasingDoubleKeyFrame KeyTime="00:00:01" Value="360"/>
                <EasingDoubleKeyFrame KeyTime="00:00:02" Value="720"/>
            </DoubleAnimationUsingKeyFrames>
           
            <!--宽度按比例放大缩小,先放大到4位,再还原-->
            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="img" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)">
                <EasingDoubleKeyFrame KeyTime="00:00:01" Value="4"/>
                <EasingDoubleKeyFrame KeyTime="00:00:02" Value="1"/>
            </DoubleAnimationUsingKeyFrames>
           
            <!--高度按比例放大缩小,先放大到4位,再还原-->
            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="img" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)">
                <EasingDoubleKeyFrame KeyTime="00:00:01" Value="4"/>
                <EasingDoubleKeyFrame KeyTime="00:00:02" Value="1"/>
            </DoubleAnimationUsingKeyFrames>           
           
            <!--不透明度变化,从1到0.1,再还原为1-->
            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="img" Storyboard.TargetProperty="(UIElement.opacity)">
                <EasingDoubleKeyFrame KeyTime="00:00:01" Value="0.1"/>
                <EasingDoubleKeyFrame KeyTime="00:00:02" Value="1"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
       
    </navigation:Page.resources>
    <Grid x:Name="LayoutRoot">
        <Image x:Name="img" source="/Projection;Component/img/img0.jpg" Width="100" HorizontalAlignment="Center" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5">
            <Image.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform/>
                    <TranslateTransform/>
                </TransformGroup>
            </Image.RenderTransform>
            <Image.Projection>
                <PlaneProjection/>
            </Image.Projection>
            <Image.Effect>
                <DropShadowEffect/>
            </Image.Effect>
        </Image>
    </Grid>
</navigation:Page>

结构不复杂,里面就放了一张图片,同时预置了一个动画storyTest,里面把几种常见的动画形式都列在里面了,下面就来看看如何动态改变storyTest动画的属性(比如改变旋转的起始角度之类)

1.直接加x:Name,然后引用赋值

代码

<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="img" Storyboard.TargetProperty="(UIElement.projection).(PlaneProjection.RotationY)">
    <EasingDoubleKeyFrame KeyTime="00:00:01" Value="360" x:Name="yFrom"/>
    <EasingDoubleKeyFrame KeyTime="00:00:02" Value="720" x:Name="yTo"/>
</DoubleAnimationUsingKeyFrames>

注意其中的x:Name="yFrom"设置,然后象这样引用

代码

System.Windows.Media.Animation.EasingDoubleKeyFrame yFrom = this.FindName("yFrom") as System.Windows.Media.Animation.EasingDoubleKeyFrame;
yFrom.Value = 20;

这样我们就把00:00:01秒时角度由360改为20度了

2.利用StoryBoard的Children属性

代码

DoubleAnimationUsingKeyFrames _rotate = storyTest.Children[0] as DoubleAnimationUsingKeyFrames;
_rotate.KeyFrames[0].Value = 90;//修改旋转角度的值
_rotate.KeyFrames[1].Value = 180;
storyTest.RepeatBehavior = RepeatBehavior.Forever;//指定为一直播放
//this.storyTest.RepeatBehavior = new RepeatBehavior(2);//播放2次

这样我们就把旋转的角度值从360,720改为90,180了

说明:StoryBoard的Children属性得到的是一个Timeline的集合,而DoubleAnimationUsingKeyFrames 等这些基本的关键帧类型都是继承自Timeline,因此可以用as安全的把Timeline向下转化为 DoubleAnimationUsingKeyFrames

3.动态添加/删除关键帧

当xaml中预定义的关键帧不满足要求时,这种方法就几乎成了唯一选择

代码

storyTest.Children.Clear();//清除原来的动画设定,仅保留一个空壳          
DoubleAnimationUsingKeyFrames _new_rotate = new DoubleAnimationUsingKeyFrames();
EasingDoubleKeyFrame _frame1 = new EasingDoubleKeyFrame();
_frame1.Value = 180;
_frame1.KeyTime = new TimeSpan(0,1);
EasingDoubleKeyFrame _frame2 = new EasingDoubleKeyFrame();
_frame2.Value = 0;
_frame2.KeyTime = new TimeSpan(0,2);
EasingDoubleKeyFrame _frame3 = new EasingDoubleKeyFrame();
_frame3.Value = 90;
_frame3.KeyTime = new TimeSpan(0,5);
_new_rotate.KeyFrames.Add(_frame1);
_new_rotate.KeyFrames.Add(_frame2);
_new_rotate.KeyFrames.Add(_frame3);
storyTest.Children.Add(_new_rotatE);
Storyboard.SetTarget(_new_rotate,this.img);
Storyboard.SetTargetProperty(_new_rotate,new PropertyPath("(UIElement.projection).(PlaneProjection.RotationY)"));
storyTest.AutoReverse = false;
//storyTest.FillBehavior = FillBehavior.Stop;//加入这一行后,不管AutoReverse设置为何值,都会回复原状           
storyTest.begin();

4.获得所有关键帧

代码

TimelineCollection m_SotryInKeyFrames = storyTest.Children;

然后循环,代码

foreach (DoubleAnimationUsingKeyFrames keyFrame in m_SotryInKeyFrames)

目的在于可动态设置doubleAnimation的动画对象,如代码

foreach (DoubleAnimationUsingKeyFrames keyFrame in m_SotryInKeyFrames)
{
    Storyboard.SetTarget(keyFrame,usercontrol);
}

呵呵,欢迎转载~~~~

大佬总结

以上是大佬教程为你收集整理的silverlight中用代码动态控制Storyboard(动画)属性的几种方法全部内容,希望文章能够帮你解决silverlight中用代码动态控制Storyboard(动画)属性的几种方法所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。