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

标签 wpf fullscreen double-click

我一直在寻找一种简单的方法来使我的窗口(仅包含 mediaElement)在双击时全屏显示。由于我是 WPF/C# 新手,我按照建议的方式进行了操作 here 。它可以工作,但它并不总是有反应,有时我什至必须连续单击 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/

关于WPF 在 mediaElement 上双击进入全屏并不总是有反应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8821211/

相关文章:

c# - 在 C# 中执行 WPF 元素验证?

c# - WPF - 如何与动态创建的控件实现双向数据绑定(bind)?

javascript - 从全屏按下转义时更改 CSS 类

css - 中心 3 div 水平全屏

Excel:触发双击事件

wpf - 将 WPF MediaElement 与 MKV 视频格式一起使用 - 无音频

c# - XAML 中的对象初始值设定项

javascript - 猫头鹰轮播全屏不起作用

ios - 双击主页按钮模糊应用程序屏幕截图

java - 检测多次双击/重置 MouseEvent?