WPF媒体元素双击全屏不总是起作用

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

    }

两个提示:1. 在事件处理程序中放置跟踪点或 Debug.WriteLine(...) 调用,以查看每次双击时是否调用了它。2. 在条件语句中不需要显式的布尔比较,只需使用 if (... && !fullscreen)if (... && fullscreen) - Samuel Slade
我必须像一个狂人一样点击才能执行处理程序。每次进入时都会正确执行,但问题是为什么我要连续点击两次才能进入它。谢谢你的建议。 - HomeMade
我用另一种方法解决了这个问题。使用这个解决方案。 - HomeMade
2个回答

4
以下代码演示了如何通过在媒体元素上双击使窗口全屏。只需在DemoWindow.xaml.cs中更改"path_to_file"为适当的文件路径即可。
重要提示:必须设置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();
    }
}

Reference

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


-4
使用这个:
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;
            }

        }

这是最简单的方法!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接