WPF MediaElement的透明度执行后的故事情节未设置

问题描述 投票:0回答:1

我使用DoubleAnimationStoryboard控制OpacityMediaElement。动画本身工作正常,但如果我叫DisappearplayVid几秒钟后,Opacityplayer保持为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
}
c# wpf storyboard opacity doubleanimation
1个回答
2
投票

使用FillBehavior等于Stop,而且您的播放器的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的其他方法。

© www.soinside.com 2019 - 2024. All rights reserved.