gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 03:43:09 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com