作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在寻找一种简单的方法来使我的窗口(仅包含 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 中文件的适当路径
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/
我是一名优秀的程序员,十分优秀!