silverlight动画不能正常工作

时间:2011-09-13 07:03:19

标签: c# silverlight xaml animation

这是我第一次创建silverlight动画。我一直在努力创造一个简单的动画,过去几天我无法让它工作。我不敢相信为什么这么难。

的Xaml:

<UserControl x:Class="BuzzLifeAppsSilverlight.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    <UserControl.Resources>
        <Storyboard x:Name="Storyboard1" AutoReverse="True" RepeatBehavior="Forever" BeginTime="1">
            <DoubleAnimation Duration="0:0:2" To="-332" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateX)" Storyboard.TargetName="button1" d:IsOptimized="True"/>
            <DoubleAnimation Duration="0:0:2" To="-12" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateY)" Storyboard.TargetName="button1" d:IsOptimized="True"/>
        </Storyboard>
    </UserControl.Resources>

    <Canvas x:Name="LayoutRoot" Background="White">
        <Button Canvas.Left="235" Canvas.Top="136" Content="Button" Height="23" Name="button1" Width="75" RenderTransformOrigin="0.5,0.5" >
            <Button.RenderTransform>
                <CompositeTransform/>
            </Button.RenderTransform>
        </Button>
    </Canvas>
</UserControl>

代码:

public MainPage()
{
    InitializeComponent();

    Storyboard1.Begin();
}

1 个答案:

答案 0 :(得分:4)

删除故事板中 BeginTime 的声明,或将其更改为其他值(如0:0:1)。

值为1意味着动画应该在一小时内开始,将其设置为0:0:1,将在1秒后开始动画。

当MainPage加载并添加到可视化树中时,也启动动画,就像这样..

public MainPage(){
  InitializeComponent();
  this.Loaded += new System.Windows.RoutedEventHandler(MainPage_Loaded);
}

private void MainPage_Loaded(object sender, System.Windows.RoutedEventArgs e){
  Storyboard1.Begin();
}