执行故事情节后未设置 WPF MediaElement 不透明度
WPF MediaElement Opacity is not set after executing Storyline
我正在使用 DoubleAnimation
和 Storyboard
来控制 Opacity
或 MediaElement
。动画本身工作正常,但如果我在几秒钟后调用 Disappear
和 playVid
,player
的 Opacity
仍然为 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
等于 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 了解其他方法。
我正在使用 DoubleAnimation
和 Storyboard
来控制 Opacity
或 MediaElement
。动画本身工作正常,但如果我在几秒钟后调用 Disappear
和 playVid
,player
的 Opacity
仍然为 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
等于 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 了解其他方法。