简体   繁体   English

适用于Mahapps的全屏行为

[英]FullscreenBehaviour for Mahapps

How to add a dynamic switching ability from fullscreen to windowed mode and vice versa to Mahapps MetroWindow? 如何将动态切换功能从全屏模式添加到窗口模式,反之亦然到Mahapps MetroWindow?

Starting with Normal Window 从普通窗口开始

最初的窗口状态

and after switching to fullscreen the top right window Buttons (Minimize/Maximize/Close) are still visible (but they shouldn't be visible as well as the title bar). 切换到全屏模式后,右上角的按钮(最小化/最大化/关闭)仍然可见(但标题栏应该不可见)。 The reserved space for the title bar seems to be still there. 标题栏的保留空间似乎仍然存在。

切换到全屏后按钮仍然可见

The other way round initially from fullscreen state (no buttons, except the Hello button in the middle and no title bar => as expected) 从全屏状态开始的另一种方式(没有按钮,除了中间的Hello按钮,没有标题栏=>符合预期)

在此处输入图片说明

... but when switching back to normal window state the title is back again but the top left buttons are missing. ...但是当切换回正常窗口状态时,标题又回来了,但是左上方的按钮丢失了。

在此处输入图片说明

Am I doing something wrong here in the code? 我在代码中在做错什么吗? I used an derrived Behaviour. 我使用了衍生行为。 The interesting part that is executed when switching is this: 切换时执行的有趣的部分是:

    private static void OnIsFullscreenChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        var window = (MetroWindow)sender;

        var oldValue = (bool)e.OldValue;
        var newValue = (bool)e.NewValue;

        if (newValue == oldValue || window == null)
        {
            return;
        }

        if (newValue)
        {
            window.Tag = window.WindowState;

            window.Topmost = true;

            window.UseNoneWindowStyle = true;
            window.IgnoreTaskbarOnMaximize = true;
            window.ShowTitleBar = false;

            window.WindowStyle = WindowStyle.None;
            window.WindowState = WindowState.Maximized;
        }
        else
        {
            window.Topmost = false;

            window.UseNoneWindowStyle = false;
            window.IgnoreTaskbarOnMaximize = false;
            window.ShowTitleBar = true;

            window.WindowStyle = WindowStyle.SingleBorderWindow;
            window.WindowState = (WindowState)window.Tag;
        }
    }

Attaching a simular Behaviour to a default Window WPF control everything works as expected. 将模拟行为附加到默认Window WPF控件,一切都会按预期工作。

I attach the Behaviour this way: 我以这种方式附加行为:

<controls:MetroWindow ... local:FullscreenBehavior.IsFullscreen="{Binding Fullscreen}">
<!-- code above sets initial state depending on ViewModel value -->
<!-- code below fires mode switching when a defined key is pressed => executes  OnIsFullscreenChanged method -->
    <i:Interaction.Behaviors>
        <behaviours:BorderlessWindowBehavior />
        <behaviours:WindowsSettingBehaviour />
        <behaviours:GlowWindowBehavior />
        <modern:FullscreenBehavior FullscreenKey="{Binding FullscreenKey}" />       
    </i:Interaction.Behaviors>
    ...

EDIT: Set state of Window Buttons explicitly When I extend the method to set the states to the correct value explicitly there seems to be another strange effect: 编辑:显式设置窗口按钮的状态当我扩展该方法以将状态显式设置为正确的值时,似乎还有另一个奇怪的效果:

    private static void OnIsFullscreenChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        var window = (MetroWindow)sender;

        var oldValue = (bool)e.OldValue;
        var newValue = (bool)e.NewValue;

        if (newValue == oldValue || window == null)
        {
            return;
        }

        if (newValue)
        {
            window.Tag = window.WindowState;

            window.Topmost = true;

            window.UseNoneWindowStyle = true;
            window.IgnoreTaskbarOnMaximize = true;
            window.ShowTitleBar = false;
            window.ShowCloseButton = false;
            window.ShowMaxRestoreButton = false;
            window.ShowMinButton = false;

            window.WindowStyle = WindowStyle.None;
            window.WindowState = WindowState.Maximized;
        }
        else
        {
            window.Topmost = false;

            window.UseNoneWindowStyle = false;
            window.IgnoreTaskbarOnMaximize = false;
            window.ShowTitleBar = true;
            window.ShowCloseButton = true;
            window.ShowMaxRestoreButton = true;
            window.ShowMinButton = true;

            window.ShowCloseButton = true;
            window.ShowMaxRestoreButton = true;
            window.WindowStyle = WindowStyle.SingleBorderWindow;
            window.WindowState = (WindowState)window.Tag;
        }
    }

The window gets "sometimes" cut at the border and sometimes it looks right (like in the first picture at the top). 窗口有时会在边界处被切掉,有时看起来是正确的(如顶部的第一张图片)。 Also I don't know (yet) wheter the space of the title bar is no longer reserved when initially starting with fullscreen (there seems to be a difference, don't know why). 我也不知道(当初)从全屏开始时标题栏的空间是否已不再保留(似乎有所不同,不知道为什么)。

在此处输入图片说明

There is a little bug in the current 1.0 release. 当前的1.0版本中有一个小错误。 If you toggle the UseNoneWindowStyle , it doesn't bring back the buttons and toolbar. 如果切换UseNoneWindowStyle ,它不会带回按钮和工具栏。 I'll fix this as soon as possible. 我会尽快修复。

So, here is a little workaround for you. 因此,这里有一些解决方法。

public static readonly DependencyProperty ToggleFullScreenProperty =
    DependencyProperty.Register("ToggleFullScreen",
                                typeof(bool),
                                typeof(MainWindow),
                                new PropertyMetadata(default(bool), ToggleFullScreenPropertyChangedCallback));

private static void ToggleFullScreenPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
    var metroWindow = (MetroWindow)dependencyObject;
    if (e.OldValue != e.NewValue)
    {
        var fullScreen = (bool)e.NewValue;
        if (fullScreen)
        {
            metroWindow.UseNoneWindowStyle = true;
            metroWindow.IgnoreTaskbarOnMaximize = true;
            metroWindow.ShowMinButton = false;
            metroWindow.ShowMaxRestoreButton = false;
            metroWindow.ShowCloseButton = false;
            metroWindow.WindowState = WindowState.Maximized;
        }
        else
        {
            metroWindow.UseNoneWindowStyle = false;
            metroWindow.ShowTitleBar = true; // <-- this must be set to true
            metroWindow.IgnoreTaskbarOnMaximize = false;
            metroWindow.ShowMinButton = true;
            metroWindow.ShowMaxRestoreButton = true;
            metroWindow.ShowCloseButton = true;
            metroWindow.WindowState = WindowState.Normal;
        }
    }
}

public bool ToggleFullScreen
{
    get { return (bool)GetValue(ToggleFullScreenProperty); }
    set { SetValue(ToggleFullScreenProperty, value); }
}

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM