1
4

1 に答える 1

2

アニメーション ロジックをメソッドにラップし、Loaded イベントやその他の必要なイベントから呼び出すことができます。

private void CreateAnimation()
{
    DoubleAnimation doubleAnimation = new DoubleAnimation();
    doubleAnimation.From = -LabelNameSong.ActualWidth;
    doubleAnimation.To = canMain.ActualWidth;
    doubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
    doubleAnimation.Duration = new Duration(TimeSpan.Parse("0:0:10"));
    LabelNameSong.BeginAnimation(Canvas.RightProperty, doubleAnimation);
}

void Window1_Loaded(object sender, RoutedEventArgs e)
{
    CreateAnimation();
}

private void LabelNameSong_SizeChanged(object sender, RoutedEventArgs e)
{
    CreateAnimation();
}

しかし、おそらく Triggers を使用して Xaml ですべてを実行し、すべてのコード ビハインドを取り除くことができます。

以下の例では、アニメーションはロード時に開始され、サイズが変更されると再開されます

例:

 <Label x:Name="LabelNameSong" Content="Hello" >
    <Label.Resources>
        <Storyboard x:Key="scroll">
            <DoubleAnimation To="{Binding ActualWidth, ElementName=LabelNameSong}" Duration="00:00:10"
              Storyboard.TargetProperty="(Canvas.Right)"
              Storyboard.TargetName="LabelNameSong"
              RepeatBehavior="Forever"/>
        </Storyboard>
    </Label.Resources>

    <Label.Triggers>
        <EventTrigger RoutedEvent="Label.Loaded">
            <BeginStoryboard Storyboard="{StaticResource scroll}" />
        </EventTrigger>
        <EventTrigger RoutedEvent="Label.SizeChanged">
            <BeginStoryboard Storyboard="{StaticResource scroll}" />
        </EventTrigger>
    </Label.Triggers>
</Label>
于 2013-08-21T00:10:19.883 に答える