FullscreenBehaviour for Mahapps

时间:2015-01-19 10:16:32

标签: c# wpf fullscreen mahapps.metro

如何将全屏到窗口模式的动态切换功能添加到Mahapps MetroWindow?

从普通窗口开始

Initially windowed state

在切换到全屏后,右上方的窗口按钮(最小化/最大化/关闭)仍然可见(但它们不应该与标题栏一样可见)。标题栏的保留空间似乎仍然存在。

Buttons still visible after switching to fullscreen

另一种方式是从全屏状态开始(没有按钮,除了中间的Hello按钮,没有标题栏=>如预期的那样)

enter image description here

...但是当切换回正常窗口状态时,标题会再次返回,但左上角的按钮会丢失。

enter image description here

我在代码中做错了吗?我使用了一种嗜好的行为。切换时执行的有趣部分是:

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

将模拟行为附加到默认窗口WPF控件一切都按预期工作。

我以这种方式附加行为:

<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>
    ...

编辑:明确设置窗口按钮的状态 当我扩展方法以明确地将状态设置为正确的值时,似乎有另一种奇怪的效果:

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

窗口得到&#34;有时&#34;在边界处切割,有时它看起来正确(就像在顶部的第一张图片中)。 此外,我还不知道(当)最初以全屏开始时标题栏的空间不再被保留(似乎有所不同,不知道为什么)。

enter image description here

1 个答案:

答案 0 :(得分:4)

当前1.0版本中存在一个小错误。如果您切换UseNoneWindowStyle,它不会带回按钮和工具栏。我会尽快解决这个问题。

所以,这里有一个小解决方法。

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

希望这有帮助。