在Windows应用商店中使用C#向foreOut添加fadeOut

时间:2013-08-16 16:29:47

标签: c# xaml animation windows-store-apps

我用C#和XAML编写了一个Windows应用商店应用程序,并尝试将fadeOut动画添加到图像中。

如果我像这样在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是错误的,但我不知道正确的方法,也没有任何错误消息。

谢谢, 克劳斯

1 个答案:

答案 0 :(得分:0)

问题是您尚未将动画添加到Storyboard。你需要:

storyboard.Children.Add(fadeOut);

另外,要稍微清理一下代码,FadeOutThemeAnimationdescribed on MSDN

  

表示适用的预配置不透明度动画   控制何时从UI中删除或隐藏它们。

您可以删除Storyboard.SetTargetProperty。动画为您处理正确的属性。你没有在XAML中设置它,所以要在C#中重新创建它,你不要设置它。