【发布时间】:2016-08-18 20:34:06
【问题描述】:
我有一个有效的 XAML 动画,用于为类似里程表的数字设置动画。我需要制作六个几乎相同但不同的动画版本。据我所知,没有一种以这种方式重用动画 XAML 的好方法,因此为了保持代码干燥,我转向了动画的程序化创建。
在尝试将此 XAML 动画转换为 C# 时,我遇到了无法找到目标的问题:
未检测到已安装的组件。\r\n\r\n无法解析 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;
}
【问题讨论】:
-
这可以是 PITA。通常,如果我需要与代码隐藏中的动画进行交互,我会做什么,我只是将它定义为 xaml 中的资源,然后从我的代码隐藏中的资源中获取动画。省去转换的麻烦。
标签: c# xaml animation uwp uwp-xaml