如何触发隐式合成动画

时间:2018-02-16 11:00:06

标签: c# uwp windows-community-toolkit

按照我find的唯一文档,我尝试了以下XAML:

<Image Name="testImage" Source="https://upload.wikimedia.org/wikipedia/commons/c/c4/PM5544_with_non-PAL_signals.png" Width="800">
    <animations:Implicit.Animations>
        <animations:ScalarAnimation Target="Translation.Y" ImplicitTarget="Offset" Duration="0:0:4"/>
        <animations:ScalerAnimation Target="Opacity" Duration="0:0:4" />
    </animations:Implicit.Animations>
</Image>

以下代码有效,不透明度设置动画并需要4秒才能淡出:

private void myButton_Tapped(object sender, TappedRoutedEventArgs e)
{
    testImage.Opacity = 0.2;
}

但我无法弄清楚如何触发翻译动画(或旋转或缩放)。我试过这个:

private void myButton_Tapped(object sender, TappedRoutedEventArgs e)
{
    var translate = new TranslateTransform();
    translate.Y = 400;
    testImage.RenderTransform = translate;
}

而且这也希望隐含会覆盖持续时间,但没有运气:

private void myButton_Tapped(object sender, TappedRoutedEventArgs e)
{
    testImage.Offset(0, 400, 0).Start();
}

更改将应用​​于这些,但没有4秒动画。

1 个答案:

答案 0 :(得分:2)

TranslateTransformRenderTransform,与您想要实现的Composition转换无关,Offset隐式动画会覆盖XAML设置。

要使其有效,您需要抓住ElementVisual,然后直接更改Offset

private void myButton_Tapped( object sender, TappedRoutedEventArgs e )
{
    var imageVisual = ElementCompositionPreview.GetElementVisual( testImage );
    imageVisual.Offset = new Vector3( 0, 400, 0 );
}