WPF在mediaElement上全屏输入双击并不总是有反应



我一直在寻找一种简单的方法,让我的窗口(只包含一个mediaElement)在双击时变成全屏。由于我是WPF/C#的新手,所以我按照这里建议的方式来做。它是有效的,但它并不总是做出反应,有时我甚至不得不连续点击3次以上才能将其全屏显示或恢复。

这是事件处理程序:

 private void mediaElement1_MouseDown(object sender, MouseButtonEventArgs e)
    {
        if (e.ClickCount == 2 && fullscreen==false)
        {
            this.WindowStyle = WindowStyle.None;
            this.WindowState = WindowState.Maximized;
        }
        else if (e.ClickCount == 2 && fullscreen == true)
        {
            this.WindowStyle = WindowStyle.SingleBorderWindow;
            this.WindowState = WindowState.Normal;
        }
        fullscreen = !fullscreen;
    }

下面的代码演示了如何通过双击媒体元素使窗口全屏显示。只需将"path_to_file"更改为DemoWindow.xaml.cs 中文件的适当路径

Importart:MediaElement的Source属性必须设置为启用MouseLeftButtonUp事件。否则,不会调用事件处理程序


DemoWindow.xaml

<Window x:Class="FullscreenDemo.DemoWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="DemoWindow" Height="300" Width="300">
    <Grid>
        <MediaElement x:Name="MediaPlayer"
                      MouseLeftButtonUp="MediaPlayer_MouseLeftButtonUp" />
    </Grid>
</Window>

DemoWindow.xaml.cs

using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
namespace FullscreenDemo
{
    public partial class DemoWindow : Window
    {
        private bool fullscreen = false;
        private DispatcherTimer DoubleClickTimer = new DispatcherTimer();
        public DemoWindow()
        {
            InitializeComponent();
            DoubleClickTimer.Interval = TimeSpan.FromMilliseconds(GetDoubleClickTime());
            DoubleClickTimer.Tick += (s, e) => DoubleClickTimer.Stop();
            var path = @"path_to_file";
            MediaPlayer.Source = new Uri(path);
        }
        private void MediaPlayer_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (!DoubleClickTimer.IsEnabled)
            {
                DoubleClickTimer.Start();
            }
            else
            {
                if (!fullscreen)
                {
                    this.WindowStyle = WindowStyle.None;
                    this.WindowState = WindowState.Maximized;
                }
                else
                {
                    this.WindowStyle = WindowStyle.SingleBorderWindow;
                    this.WindowState = WindowState.Normal;
                }
                fullscreen = !fullscreen;
            }
        }
        [DllImport("user32.dll")]
        private static extern uint GetDoubleClickTime();
    }
}

参考

https://diptimayapatra.wordpress.com/2010/03/04/full-screen-view-for-media-element-in-wpf/

使用此:

private void mediaElement1_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2 && fullscreen==false)
            {
                this.WindowStyle = WindowStyle.None;
                this.WindowState = WindowState.Maximized;
    fullscreen == true;
            }
            else if (e.ClickCount == 2 && fullscreen == true)
            {
                this.WindowStyle = WindowStyle.SingleBorderWindow;
                this.WindowState = WindowState.Normal;
    fullscreen == false;
            }
        }

这是最简单的方法!

最新更新