需求是点击按钮后,弹出弹窗播放视频。按钮的点击事件如下。
public void ShowVideo() { Window window = new Window(); window.Width = 800; window.Height = 600; // 控制弹出位置在屏幕正中 double screenHeight = SystemParameters.FullPrimaryScreenHeight; double screenWidth = SystemParameters.FullPrimaryScreenWidth; window.Top = (screenHeight - window.Height) / 2; window.Left = (screenWidth - window.Width) / 2; MediaElement player = new MediaElement(); //player.Margin = new Thickness(1, 1, 1, 1); player.Width = 800; player.Height = 600;
// 视频资源放在Debugin目录下 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(); }
运行效果如下: