WPF 媒体元素不透明度在执行故事情节后未设置



我正在使用DoubleAnimationStoryboard来控制MediaElementOpacity。动画本身工作正常,但是如果我在几秒钟后调用DisappearplayVid,则playerOpacity保持为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
}
使用等于

Stop FillBehavior,但也要将玩家的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;
};

有关其他方法,请参阅此帖子。

最新更新