繁体   English   中英

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

[英]Creating Storyboard with DoubleAnimationUsingKeyFrames in Code Behind C#

这是我完美运行的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"/>

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

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

启动故事板是缺乏的:

被放到最后。

storyboard.Begin();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM