我用 C# 和 XAML 编写了一个 Windows 商店应用程序,并尝试将淡出动画添加到图像中。
如果我像这样在 XAML 代码中添加动画,它可以工作,我可以使用“RemoveMatchsticks.Begin();”开始动画 在 C# 中
<StackPanel Orientation="Horizontal" x:Name="Matches" Height="200">
<StackPanel.Resources>
<Storyboard x:Name="RemoveMatchsticks">
<FadeOutThemeAnimation Storyboard.TargetName="matchstick" />
</Storyboard>
</StackPanel.Resources>
<Image Source="Assets/Match.png" x:Name="matchstick" />
</StackPanel>
但我需要在 C# 中添加动画。为了测试,我在 XAML 中有相同的图片
<StackPanel Orientation="Horizontal" x:Name="Matches" Height="200">
<Image Source="Assets/Match.png" x:Name="matchstick" />
</StackPanel>
和以下 c#-Code
Storyboard storyboard = new Storyboard();
FadeOutThemeAnimation fadeOut = new FadeOutThemeAnimation();
Duration duration = new Duration(TimeSpan.FromSeconds(2));
fadeOut.Duration = duration;
storyboard.Duration = duration;
Storyboard.SetTargetProperty(fadeOut, "Opacity");
Storyboard.SetTarget(fadeOut, matchstick);
storyboard.Begin();
它不起作用。我认为,SetTargetProperty 中的 targetProperty 是错误的,但我不知道正确的方法,也没有任何错误消息。
谢谢,克劳斯