gpt4 book ai didi

c# - Mahapps 的全屏行为

转载 作者:太空宇宙 更新时间:2023-11-03 21:24:36 30 4
gpt4 key购买 nike

如何为 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;
}
}

将模拟行为附加到默认 Window 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;
}
}

窗口“有时”在边界处被切割,有时看起来是正确的(如顶部的第一张图片)。我也不知道(还)标题栏的空间是否在最初以全屏启动时不再保留(似乎有区别,不知道为什么)。

enter image description here

最佳答案

当前的 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); }
}

希望这对您有所帮助。

关于c# - Mahapps 的全屏行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28022262/

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