问题描述:

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;

}

网友答案:

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/

网友答案:

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 !

相关阅读:
Top