在C#后面的代码中使用DoubleAnimationUsingKeyFrame创建故事板

时间:2015-05-07 11:12:25

标签: c# wpf xaml animation storyboard

这是我完美运作的XAML:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" 
        xmlns:local="clr-namespace:WpfApplication1" 
        x:Class="WpfApplication1.MainWindow"
        x:Name="Window"
        xmlns:converter="clr-namespace:WpfApplication1.Image"
        Title="MainWindow" Height="350" Width="525"
        Loaded="MainWindow_OnLoaded">
    <Window.Resources>
        <Storyboard x:Key="Move" RepeatBehavior="Forever">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(local:FloatingTextbox.Position)" Storyboard.TargetName="FloatingTextbox">
                <EasingDoubleKeyFrame KeyTime="0" />
                <EasingDoubleKeyFrame KeyTime="0:0:6" Value="300"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
        <converter:InitialPositionConverter x:Key="InitialPositionConverter"/>
    </Window.Resources>
    <Grid Background="Black">
        <Canvas Height="100" Width="411.111" Margin="45.833,80.556,0,0" Background="White">
            <local:FloatingTextbox x:Name="FloatingTextbox" Height="37.222" 
                                   TextWrapping="Wrap" Text="FloatingTextbox" Width="100" 
                                   BorderThickness="1" Position="1" Canvas.Top="26.389">
                <Canvas.Left>
                    <MultiBinding Converter="{StaticResource InitialPositionConverter}" Mode="OneWay">
                        <Binding Path="Position" RelativeSource="{RelativeSource Self}"/>
                        <Binding Path="ActualWidth" RelativeSource="{RelativeSource Self}"/>
                    </MultiBinding>
                </Canvas.Left>
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Loaded">
                        <ei:ControlStoryboardAction Storyboard="{StaticResource Move}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </local:FloatingTextbox>
        </Canvas>
    </Grid>
</Window>

我想创建故事板并在Code中实现动画,我试过这个:

private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
        {
            var storyboard = new Storyboard();
            storyboard.RepeatBehavior = new RepeatBehavior(4);

            var myDoubleAnimationUsingKeyFrames = new DoubleAnimationUsingKeyFrames();

            var myEasingDoubleKeyFrame1 = new EasingDoubleKeyFrame();
            myEasingDoubleKeyFrame1.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0));

            var myEasingDoubleKeyFrame2 = new EasingDoubleKeyFrame();
            myEasingDoubleKeyFrame2.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(6));
            myEasingDoubleKeyFrame2.Value = this.ActualWidth;

            myDoubleAnimationUsingKeyFrames.KeyFrames.Add(myEasingDoubleKeyFrame1);
            myDoubleAnimationUsingKeyFrames.KeyFrames.Add(myEasingDoubleKeyFrame2);

            Storyboard.SetTargetProperty(myDoubleAnimationUsingKeyFrames, new PropertyPath(FloatingTextbox.PositionProperty));
            Storyboard.SetTargetName(myDoubleAnimationUsingKeyFrames, FloatingTextbox.Name);

            storyboard.Children.Add(myDoubleAnimationUsingKeyFrames);

            var eventTrigger = new System.Windows.Interactivity.EventTrigger() { EventName = "Loaded" };

            var triggers = Interaction.GetTriggers(FloatingTextbox);
            triggers.Add(eventTrigger);

            var controlStoryboardAction = new ControlStoryboardAction();
            controlStoryboardAction.Storyboard = storyboard;
            controlStoryboardAction.ControlStoryboardOption = ControlStoryboardOption.Play;
            eventTrigger.Actions.Add(controlStoryboardAction);
        }

注意:我想在后面的代码中实现这一点的原因是在这里放置窗口ActualWidth而不是300

<EasingDoubleKeyFrame KeyTime="0:0:6" Value="300"/>

我无法绑定,因为它可以冻结,所以......这就是原因。

我的C#代码不起作用,我应该忘记什么?

1 个答案:

答案 0 :(得分:2)

启动故事板是缺乏的:

要放在最后。

storyboard.Begin();