0

TextBlock を回転させ、拡大してから縮小したい。Stroyboard を使用してコード ビハインド ファイルにコードを記述したいと考えています。

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded_1">
    <Grid>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
            <TextBlock Name="rotatingTextBlock" Text="Hello" RenderTransformOrigin="0.5, 0.5"/>
            <TextBlock Text=" World!"/>
        </StackPanel>
    </Grid>
</Window>

..

private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
    TransformGroup tc = new TransformGroup();
    RotateTransform rotateTransform = new RotateTransform();
    tc.Children.Add(rotateTransform);
    ScaleTransform scaleTransform = new ScaleTransform();
    tc.Children.Add(scaleTransform);

    rotatingTextBlock.RenderTransform = tc;

    Storyboard storyboard = new Storyboard();
    DoubleAnimation rotateAnimation = new DoubleAnimation(0, 360, new Duration(TimeSpan.FromSeconds(2)));
    rotateAnimation.AccelerationRatio = 0.15;
    rotateAnimation.DecelerationRatio = 0.15;
    Storyboard.SetTarget(rotateAnimation, rotateTransform);
    Storyboard.SetTargetProperty(rotateAnimation, new PropertyPath(RotateTransform.AngleProperty));
    storyboard.Children.Add(rotateAnimation);

    DoubleAnimation scaleAnimation = new DoubleAnimation(1, 1.2, new Duration(TimeSpan.FromSeconds(1)));
    scaleAnimation.AccelerationRatio = 0.5;
    scaleAnimation.DecelerationRatio = 0.5;
    scaleAnimation.AutoReverse = true;
    Storyboard.SetTarget(scaleAnimation, scaleTransform);
    Storyboard.SetTargetProperty(scaleAnimation, new PropertyPath(ScaleTransform.ScaleXProperty));
    storyboard.Children.Add(scaleAnimation);

    storyboard.Begin();
}

コードが機能しません。何か不足していますか?

4

2 に答える 2

1

以下の作品:

Storyboard storyboard = new Storyboard();
DoubleAnimation rotateAnimation = new DoubleAnimation(0, 360, new Duration(TimeSpan.FromSeconds(2)));
rotateAnimation.AccelerationRatio = 0.15;
rotateAnimation.DecelerationRatio = 0.15;
try { UnregisterName("rotateTransform"); }
catch { }
finally { RegisterName("rotateTransform", rotateTransform); }
Storyboard.SetTargetName(rotateAnimation, "rotateTransform");
Storyboard.SetTargetProperty(rotateAnimation, new PropertyPath(RotateTransform.AngleProperty));
storyboard.Children.Add(rotateAnimation);

DoubleAnimation scaleAnimation = new DoubleAnimation(1, 1.2, new Duration(TimeSpan.FromSeconds(1)));
scaleAnimation.AccelerationRatio = 0.5;
scaleAnimation.DecelerationRatio = 0.5;
scaleAnimation.AutoReverse = true;
try { UnregisterName("scaleTransform"); }
catch { }
finally { RegisterName("scaleTransform", scaleTransform); }
Storyboard.SetTargetName(scaleAnimation, "scaleTransform");
Storyboard.SetTargetProperty(scaleAnimation, new PropertyPath(ScaleTransform.ScaleXProperty));
storyboard.Children.Add(scaleAnimation);

storyboard.Begin(this);

ただし、LPLのソリューションを試してみましたが、それも機能します-LPLの方法が「抽象的な」ターゲットに対して機能する場合、ソリューションとしてそれをはるかに好むと思います(名前の登録などを節約するため)。(問題は、これらのプロパティ パスをまとめる方法がわからないことです!)

于 2016-01-07T15:29:55.590 に答える
1

Storyboard.SetTargetによると、UIElements でのみ機能しますが、例外をスローしないTransformため、直接ターゲットにすることはできないようです。TextBlockを右でターゲットにすると、次のPropertyPathように機能します。

Storyboard storyboard = new Storyboard();
DoubleAnimation rotateAnimation = new DoubleAnimation(0, 360, new Duration(TimeSpan.FromSeconds(2)));
rotateAnimation.AccelerationRatio = 0.15;
rotateAnimation.DecelerationRatio = 0.15;
Storyboard.SetTarget(rotateAnimation, rotatingTextBlock);
Storyboard.SetTargetProperty(rotateAnimation, new PropertyPath("RenderTransform.Children[0].(RotateTransform.Angle)"));
storyboard.Children.Add(rotateAnimation);

DoubleAnimation scaleAnimation = new DoubleAnimation(1, 1.2, new Duration(TimeSpan.FromSeconds(1)));
scaleAnimation.AccelerationRatio = 0.5;
scaleAnimation.DecelerationRatio = 0.5;
scaleAnimation.AutoReverse = true;
Storyboard.SetTarget(scaleAnimation, rotatingTextBlock);
Storyboard.SetTargetProperty(scaleAnimation, new PropertyPath("RenderTransform.Children[1].(ScaleTransform.ScaleX)"));
storyboard.Children.Add(scaleAnimation);
于 2013-02-16T14:37:01.650 に答える