【发布时间】:2017-10-31 14:35:53
【问题描述】:
有没有办法(C# 或 XAML)即使在我之前在桌面上调整大小并关闭 UWP 应用程序窗口后也可以最大化它?
我已尝试使用 ApplicationViewWindowingMode.FullScreen,但这会使应用程序全屏显示并覆盖 Windows 任务栏。
【问题讨论】:
标签: c# xaml uwp windows-10 uwp-xaml
有没有办法(C# 或 XAML)即使在我之前在桌面上调整大小并关闭 UWP 应用程序窗口后也可以最大化它?
我已尝试使用 ApplicationViewWindowingMode.FullScreen,但这会使应用程序全屏显示并覆盖 Windows 任务栏。
【问题讨论】:
标签: c# xaml uwp windows-10 uwp-xaml
您可以使用ApplicationViewWindowingMode 中的另一个值PreferredLaunchViewSize,然后设置ApplicationView.PreferredLaunchViewSize,但关键是要找出大小 将是什么。
理论上,您可以使用一个非常大的数字,并且窗口会扩展到它可能的最大值。但是,以有效像素为单位计算屏幕尺寸可能更安全。
因此,如果您只是在主Page 上调用以下方法之前 InitializeComponent();,它应该在启动时最大化窗口。
private static void MaximizeWindowOnLoad()
{
// Get how big the window can be in epx.
var bounds = ApplicationView.GetForCurrentView().VisibleBounds;
ApplicationView.PreferredLaunchViewSize = new Size(bounds.Width, bounds.Height);
ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
}
请注意,即使在您将其卸载后,该应用也会以某种方式记住这些设置。如果您想改回默认行为(应用以之前的窗口大小启动),只需调用一次 ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.Auto; 并删除所有代码。
看起来在最新的 Windows 10 版本中,ApplicationView.GetForCurrentView().VisibleBounds 不再返回以有效像素为单位的 完整 窗口大小。所以我们现在需要一种新的方法来计算它。
事实证明它非常简单,因为 DisplayInformation 类还为我们提供了屏幕分辨率和比例因子。
以下是更新后的代码-
public MainPage()
{
MaximizeWindowOnLoad();
InitializeComponent();
void MaximizeWindowOnLoad()
{
var view = DisplayInformation.GetForCurrentView();
// Get the screen resolution (APIs available from 14393 onward).
var resolution = new Size(view.ScreenWidthInRawPixels, view.ScreenHeightInRawPixels);
// Calculate the screen size in effective pixels.
// Note the height of the Windows Taskbar is ignored here since the app will only be given the maxium available size.
var scale = view.ResolutionScale == ResolutionScale.Invalid ? 1 : view.RawPixelsPerViewPixel;
var bounds = new Size(resolution.Width / scale, resolution.Height / scale);
ApplicationView.PreferredLaunchViewSize = new Size(bounds.Width, bounds.Height);
ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
}
}
【讨论】:
App 构造函数之前创建了应用程序窗口。 (2) VisibleBounds 只是返回窗口的大小。 (使用Microsoft.NETCore.UniversalWindowsPlatformv6.0.1)
VisibleBounds returns "the visible region of the window (app view)",不是屏幕尺寸,即不是“窗口可以有多大”。我尝试让它工作(并且可能在将来),此时我会编辑或添加我自己的答案,但现在我只是留下评论提供更多信息(如果你相信,请随时将其包含在你的答案中这是正确的)。
VisibleBounds 不再返回 epx 中的完整窗口大小。请参阅我的更新答案。
如果您想在启动时最大化您的应用,您可以使用以下方法:
ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.Maximized;
但请务必将其放入页面的 Loaded 事件中,否则将无法正常工作!
【讨论】:
我的分数太少,无法直接发表评论。以上都没有为我调整为最大化视图(或下面的单行 ApplicationViewWindowingMode.Maximized 方法),但我已经使用了一些答案来提出对我有用的东西。然而,它仍然非常笨重。 “DisplayInformation”中给出的屏幕尺寸太大,无法直接调整页面大小。尝试这样做没有用,我不得不将高度和宽度降低 60 以使其返回“真”,因此我有以下一些废话,它可能会帮助其他人找到更好的答案。它进入页面/窗口加载事件。无需在其他地方添加任何其他内容。
private void Page_Loaded(object sender, RoutedEventArgs e)
{
var view = ApplicationView.GetForCurrentView();
var displayInfo = DisplayInformation.GetForCurrentView();
double x = ActualWidth;
double y = ActualHeight;
bool answer = true;
// Get the screen resolution (APIs available from 14393 onward).
var resolution = new Size(displayInfo.ScreenWidthInRawPixels-60, displayInfo.ScreenHeightInRawPixels-60);
answer = view.TryResizeView(resolution); //This should return true if the resize is successful
if (answer)
{
x = displayInfo.ScreenWidthInRawPixels - 60;
y = displayInfo.ScreenHeightInRawPixels - 60;
}
answer = true;
while (answer == true)
{
x++;
answer = view.TryResizeView(new Size { Width = x, Height = y });
}
x = x - 1;
answer = true;
while (answer == true)
{
y++;
answer = view.TryResizeView(new Size { Width = x, Height = y });
}
【讨论】:
我有一个衬里,它可以像我预期的贾斯汀斯代码一样工作,但由于某种原因,当使用贾斯汀斯答案时,我的窗口不会被最大化......但后来我改变了一些让它最大化但我失去了一切我的流利设计,如亚克力和 RevealHighlite...
所以我想出了一个让我所有流畅的设计原则都满意的衬里:
ApplicationView.GetForCurrentView().TryEnterFullScreenMode();
注意事项:
我确实尝试过 Justins 的回答,并且我正在使用他在 initializeComponent() 之后直接调用的 MaximizeWindowOnLoad() 方法;
完整概述:
public class()
{
this.InitializeComponent();
MaximizeWindowOnLoad();
}
private static void MaximizeWindowOnLoad()
{
ApplicationView.GetForCurrentView().TryEnterFullScreenMode();
}
【讨论】: