Это мой XAML, который работает отлично:Создание раскадровки с DoubleAnimationUsingKeyFrames в код C#
<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>
Я хочу создать раскадровку и реализовать анимацию в коде, я попытался это:
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# не работает, что мне не хватает?