将 UWP XAML 动画转换为 C#

Converting UWP XAML animation to C#

我有一个有效的 XAML 动画,用于为类似里程表的数字设置动画。我需要制作六个几乎相同但不同的动画版本。据我所知,没有一种以这种方式重用动画 XAML 的好方法,因此为了保持代码干爽,我转而以编程方式创建动画。

在尝试将此 XAML 动画转换为 C# 时,我 运行 遇到了无法找到我的目标的问题:

No installed components were detected.\r\n\r\nCannot resolve TargetName Digit1.

如果我调用 Storyboard.SetTarget() 而不是 Storyboard.SetTargetName(),我将不再遇到此异常,但屏幕上仍然没有任何反应。

谁能告诉我这个翻译哪里做错了?

XAML:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      mc:Ignorable="d">

    <Page.Resources>
        <x:Double x:Key="OdometerMoveUpDistance">-101</x:Double>

        <Storyboard x:Name="OdometerUpStoryboard">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateY)"
                                           Storyboard.TargetName="Digit1">
                <EasingDoubleKeyFrame KeyTime="0" Value="0" />
                <EasingDoubleKeyFrame KeyTime="0:0:1" Value="{StaticResource OdometerMoveUpDistance}" />
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
    </Page.Resources>

    <Canvas x:Name="Odometer" Width="100" Height="100">
        <TextBlock x:Name="Digit1" FontSize="100" Text="8">
            <TextBlock.RenderTransform><CompositeTransform /></TextBlock.RenderTransform>
        </TextBlock>
    </Canvas>

</Page>

C#:

private void Btn_Click(object sender, RoutedEventArgs e)
{
    DoubleAnimationUsingKeyFrames digitUp = CreateOdometerdigitAnim(this.Digit1, 0, -101);

    var storyboard = new Storyboard();
    storyboard.Children.Add(digitUp);
    storyboard.Begin(); // Exception happens on this line
}

private DoubleAnimationUsingKeyFrames CreateOdometerdigitAnim(TextBlock digit, double startPos, double endPos)
{
    var start = new EasingDoubleKeyFrame();
    start.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0));
    start.Value = startPos;

    var end = new EasingDoubleKeyFrame();
    end.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(1000));
    end.Value = endPos;

    var anim = new DoubleAnimationUsingKeyFrames();
    anim.KeyFrames.Add(start);
    anim.KeyFrames.Add(end);

    Storyboard.SetTargetProperty(anim, "(UIElement.RenderTransform).(CompositeTransform.TranslateY)");
    Storyboard.SetTargetName(anim, digit.Name);

    /* Using SetTarget instead of SetTargetName throws no exception but still does not animate */
    // Storyboard.SetTarget(anim, digit);

    return anim;
}

If I call Storyboard.SetTarget() instead of Storyboard.SetTargetName() I no longer get this exception but still nothing happens on the screen.

你需要给Digit1.RenderTransform一个CompositeTransform对象。您可以在 XAML 或代码隐藏中执行此操作:

如果在XAML中:

<TextBlock x:Name="Digit1" FontSize="100" Text="8">
    <TextBlock.RenderTransform>
        <CompositeTransform />
    </TextBlock.RenderTransform>
</TextBlock>

如果在代码隐藏中,请在 storyboard.Begin() 之前添加代码,如下所示:

Digit1.RenderTransform = new CompositeTransform();
...
storyboard.Begin();

In trying to convert this XAML animation to C# I've run into an issue where my target can't be found:

No installed components were detected.\r\n\r\nCannot resolve TargetName Digit1.

我可以用你的代码重现这个问题。我用 XAML 定义的 StoryBoard+Animation 试了一下,一切正常。稍后需要通过内部渠道进行一些确认。