【问题标题】:WPF MediaElement Opacity is not set after executing Storyline执行 Storyline 后未设置 WPF MediaElement 不透明度
【发布时间】:2019-06-27 01:48:38
【问题描述】:

我正在使用DoubleAnimationStoryboard 来控制OpacityMediaElement。动画本身运行良好,但如果我在几秒钟后调用DisappearplayVidplayerOpacity 仍然为0!有什么问题?

public void playVid(string source, bool isMainVid)
{
    player.Opacity = 1;
    player.Play(); //player.Opacity is 0 here!
}

public void Disappear()
{
    DoubleAnimation fadeOut = new DoubleAnimation
    {
        To = 0,
        Duration = new Duration(TimeSpan.FromMilliseconds(1000))
    };
    fadeOut.Completed += (s, e) =>
    {
        player.Stop();
    };
    var storyboard = new Storyboard();
    storyboard.Children.Add(fadeOut);
    Storyboard.SetTargetName(fadeOut, player.Name);
    Storyboard.SetTargetProperty(fadeOut, new PropertyPath(OpacityProperty));
    storyboard.Begin(mainGrid, HandoffBehavior.SnapshotAndReplace); //mainGrid is player's parent
}

【问题讨论】:

  • 将动画的 FillBehavior 设置为停止。
  • @Clemens Now Opacity 在动画完成时重置为动画之前的值。衰减为 0 后为 1。

标签: c# wpf storyboard opacity doubleanimation


【解决方案1】:

使用等于StopFillBehavior,但还要将播放器的Opacity 设置为最终的不透明度值(在Completed 处理程序中)。否则,它将被重置为动画之前的值。

var fadeOut = new DoubleAnimation
{
    To = 0,
    Duration = new Duration(TimeSpan.FromMilliseconds(1000)),
    FillBehavior = FillBehavior.Stop
};

fadeOut.Completed += (s, e) =>
{
    player.Stop();
    player.Opacity = 0;
};

有关其他方法,请参阅此post

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-18
    • 2010-11-27
    • 1970-01-01
    • 2015-09-04
    • 1970-01-01
    • 2016-05-15
    • 1970-01-01
    • 2011-04-16
    相关资源
    最近更新 更多