日期:2014-05-16  浏览次数:21107 次

[Win8]Windows8开发笔记(十一):动画故事版 StoryBoard的入门介绍

新建一个项目叫做:TestAnimation用来测试动画StoryBoard的使用。

在上面拖拽一个Button来做实验。

<Page
    x:Class="TestAnimation.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TestAnimation"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <Button Content="Button" HorizontalAlignment="Left" Margin="200,100,0,0" VerticalAlignment="Top">
            <Button.RenderTransform>
                <ScaleTransform x:Name="st1">
                    
                </ScaleTransform>
            </Button.RenderTransform>
        </Button>

    </Grid>
</Page>

然后在前面声明一个StoryBoard的资源:

    <Page.Resources>
        <Storyboard x:Name="sb1">
            <DoubleAnimation Storyboard.TargetName="st1" 
                             Storyboard.TargetProperty="ScaleX" From="0" To="10">
            </DoubleAnimation>    
        </Storyboard>
    </Page.Resources>

双击button添加监听,启动动画,使得点击按钮的时候按钮的宽度变化10倍。

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            sb1.Begin();
        }

运行项目就可以看到效果了。

当然,动画不仅仅局限于RenderTransform,也可以用在映射上。

<Page
    x:Class="TestAnimation.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TestAnimation"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Page.Resources>
        <Storyboard x:Name="sb1">
            <DoubleAnimation Storyboard.TargetName="st1" 
                             Storyboard.TargetProperty="ScaleX" From="0" To="10">
            </DoubleAnimation>
        </Storyboard>

        <Storyboard x:Name="sb2">
            <DoubleAnimation Storyboard.TargetName="pp1" 
                             Storyboard.TargetProperty="RotationY" From="0" To="360">
            </DoubleAnimation>
        </Storyboard>
    </Page.Resources>
    
    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <Button Content="Button" HorizontalAlignment="Left" Margin="200,100,0,0" VerticalAlignment="Top" Click="Button_Click_1">
            <Button.RenderTransform>
                <Sc