【问题标题】:Image fade doubleanimation图像淡入淡出双重动画
【发布时间】:2023-04-10 09:24:02
【问题描述】:

我正在尝试在 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 更改为“1x”或“0:0:1”并将 AutoReverse 更改为“False”(我必须创建一个淡出效果在我的图像上)没有任何作用?

【问题讨论】:

  • 没找到你。你想要什么?将RepeatBehavior 设置为1x 将使动画只运行一次。这不是你想要的吗?

标签: c# wpf windows storyboard


【解决方案1】:

当您说设置 AutoReverse="False" 和 RepeatBehavior="1x" 时没有任何效果时,我有点惊讶,所以我试了一下,单次淡出效果很好。这是 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);

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2016-02-07
    • 2021-04-13
    • 2014-01-20
    • 2012-12-18
    • 2014-02-08
    • 2019-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多