最近在研究如何將視頻全屏播放,一開始思路A:彈窗將MediaElement對象add到一個新的全屏窗體,報錯
指定的元素已經是另一個元素的邏輯子元素。請先將其斷開連接。
后續轉換思路B:將本窗體其他控件隱藏掉,然后窗體最大化,去掉邊框,然后把MediaElement設置成屏幕的寬高。
點擊【播放】,加載視頻

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApplication1 { /// <summary> /// MainWindow.xaml 的交互邏輯 /// </summary> public partial class MainWindow : Window { MediaElement myPlayer = new MediaElement(); public MainWindow() { InitializeComponent(); myPlayer.Margin = new Thickness(1, 1, 1, 1); myPlayer.Width = ActualWidth; myPlayer.Height = ActualHeight; myPlayer.LoadedBehavior = MediaState.Manual; var mp4_path = AppDomain.CurrentDomain.BaseDirectory + "video.mp4"; myPlayer.Source = new Uri(mp4_path, UriKind.RelativeOrAbsolute); (Content as Grid).Children.Add(myPlayer); } void myContent_MouseDoubleClick(object sender, MouseButtonEventArgs e) { if (FullScreenHelper.IsFullscreen(this)) FullScreenHelper.ExitFullscreen(this); else FullScreenHelper.GoFullscreen(this); } private void Button_Click(object sender, RoutedEventArgs e) { myPlayer.Play(); } private void Window_SizeChanged(object sender, SizeChangedEventArgs e) { myPlayer.Width = ActualWidth; myPlayer.Height = ActualHeight; } } }
雙擊視頻,全屏播放
有需要這個效果的可以參考