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

发布于 2024-12-26 11:45:48 字数 636 浏览 1 评论 0原文

我一直在寻找一种简单的方法来使我的窗口(仅包含 mediaElement)在双击时全屏显示。由于我是 WPF/C# 新手,所以我按照建议的方式进行了操作

这是事件处理程序:

 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;

    }

I've been searching the easy way to make my window (which contains only a mediaElement) go full screen when it's double clicked. Since I'm new to WPF/C# I did it the way it was suggested here. It works, but it doesn't react always and sometimes I even have to click more than 3 times in row to get it into full screen or restored.

Here is the event handler:

 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;

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罗罗贝儿 2025-01-02 11:45:48

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

。 导入:必须设置 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/

The following code demonstrate how to make the window fullscreen by double clicking in the media element. Just change "path_to_file" for the appropriated path the file in the DemoWindow.xaml.cs

Importart: The Source property from MediaElement must be set to enable the MouseLeftButtonUp event. Otherwise the event handler is not called.


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/

苍景流年 2025-01-02 11:45:48

使用这个:

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

        }

这是最简单的方法!

Use this:

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

        }

Its the easiest way !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文