问题描述
如何将动态切换功能从全屏模式添加到窗口模式,反之亦然到Mahapps MetroWindow?
How to add a dynamic switching ability from fullscreen to windowed mode and vice versa to Mahapps MetroWindow?
从普通窗口开始
,并切换到全屏模式后,右上角的窗口按钮(最小化/最大化/关闭)仍然可见(但不应像标题栏一样显示).标题栏的保留空间似乎仍然存在.
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.
从全屏状态开始的另一种方式(没有按钮,除了中间的Hello按钮,没有标题栏=>符合预期)
The other way round initially from fullscreen state (no buttons, except the Hello button in the middle and no title bar => as expected)
...但是当切换回正常的窗口状态时,标题又回来了,但是左上方的按钮丢失了.
... 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;
}
}
将模拟行为附加到默认的Window WPF控件,一切都会按预期进行.
Attaching a simular Behaviour to a default Window WPF control everything works as expected.
我通过以下方式附加行为:
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>
...
明确设置窗口按钮"的状态当我扩展该方法以将状态显式设置为正确的值时,似乎还有另一个奇怪的效果:
Set state of Window Buttons explicitlyWhen 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).
推荐答案
当前的1.0版本中存在一个小错误.如果切换 UseNoneWindowStyle
,它不会带回按钮和工具栏.我会尽快修复.
There is a little bug in the current 1.0 release. If you toggle the UseNoneWindowStyle
, it doesn't bring back the buttons and toolbar. 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); }
}
希望这会有所帮助.
这篇关于适用于Mahapps的全屏行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!