图像淡入双重动画

时间:2014-04-02 16:59:19

标签: c# wpf windows storyboard

我试图在wpf和c#中实现对图像的淡出效果

<Image.Triggers> 
  <EventTrigger RoutedEvent="Image.Loaded">
    <BeginStoryboard>
           <Storyboard>
                 <DoubleAnimation Storyboard.TargetName="imgSlot1"
                            Storyboard.TargetProperty="Opacity"
                            From="1.0" To="0.0" Duration="0:0:1"
                            AutoReverse="True" RepeatBehavior="Forever"/>
                    </Storyboard>
                </BeginStoryboard>
  </EventTrigger>

使用此代码,我看到我的图像闪烁,这没关系,但为什么我将RepeatBehavior改为&#34; 1x&#34;或&#34; 0:0:1&#34;和自动反转到&#34;错误&#34; (我必须在我的图像上创建一个淡出淡出的效果)没有任何作用?

1 个答案:

答案 0 :(得分:1)

当你设置AutoReverse =&#34; False&#34;当你说什么都行不通时,我有点惊讶和RepeatBehavior =&#34; 1x&#34;,所以我尝试了,单个淡出效果很好。这是xaml:

        <Image.Triggers>
            <EventTrigger RoutedEvent="Image.Loaded">
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetName="imgSlot1"
                        Storyboard.TargetProperty="Opacity"
                        From="1.0" To="0.0" Duration="0:0:1"
                        AutoReverse="False" RepeatBehavior="1x"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Image.Triggers>

但是,我不确定您是否希望在Image.Loaded事件上发生这种情况。并且记住,即使没有故事板,您也可以轻松地从C#或VB控制它,类似于以下内容:

        DoubleAnimation fadeoutAnimation = new DoubleAnimation();
        fadeoutAnimation.Duration = TimeSpan.FromSeconds(1.0d);
        fadeoutAnimation.From = 1.0d;
        fadeoutAnimation.To = 0.0d;
        imgSlot1.BeginAnimation(Image.OpacityProperty, fadeoutAnimation);

希望这有帮助!