需求是点击按钮后,弹出弹窗播放视频。按钮的点击事件如下。
public void ShowVideo()
{
Window window = new Window();
window.Width = ;
window.Height = ; // 控制弹出位置在屏幕正中
double screenHeight = SystemParameters.FullPrimaryScreenHeight;
double screenWidth = SystemParameters.FullPrimaryScreenWidth;
window.Top = (screenHeight - window.Height) / ;
window.Left = (screenWidth - window.Width) / ; MediaElement player = new MediaElement();
//player.Margin = new Thickness(1, 1, 1, 1);
player.Width = ;
player.Height = ;
// 视频资源放在Debug\bin目录下
var mp4_path = AppDomain.CurrentDomain.BaseDirectory + @"\video.mp4";
player.Source = new Uri(mp4_path, UriKind.RelativeOrAbsolute);
player.LoadedBehavior = MediaState.Manual;
player.Stop();
player.Play(); // 视频播放控件加入到窗体中
window.Content = player;
window.ShowDialog();
}
运行效果如下: