WPF在mediaElement上进入全屏双击并不总是做出反应

WPF在mediaElement上进入全屏双击并不总是做出反应,wpf,fullscreen,double-click,Wpf,Fullscreen,Double Click,我一直在寻找一种简单的方法,使我的窗口(只包含一个mediaElement)在双击时全屏显示。因为我是WPF/C的新手,所以我按照建议的方式做了。它可以工作,但它并不总是有反应,有时我甚至不得不连续点击3次以上才能让它进入全屏或恢复 以下是事件处理程序: private void mediaElement1_MouseDown(object sender, MouseButtonEventArgs e) { if (e.ClickCount == 2 &&

我一直在寻找一种简单的方法,使我的窗口(只包含一个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;

    }
使用以下命令:

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;
            }

        }

这是最简单的方法

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

ImportStart:必须设置MediaElement的源属性才能启用MouseLeftButtonUp事件。否则不调用事件处理程序。
DemoWindow.xaml


参考


两个提示:1。在事件处理程序中放置一个跟踪点或
Debug.WriteLine(…)
调用,查看它是否在每次双击时被调用。2.在条件语句中不需要显式的布尔比较,只需执行
if(…&&fullscreen)
if(…&&fullscreen)
。我必须像疯子一样单击以使其执行处理程序。它每次进入时都会被正确执行,但问题是为什么每次单击两次它都不会进入。谢谢你的提示。我用另一种方法解决了这个问题。使用溶液。
<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>
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();
    }
}