有没有一种方法(使用C#或XAML),即使在桌面上先前调整大小并关闭了UWP应用程序窗口,我也可以将其最大化?
我已经尝试使用ApplicationViewWindowingMode.FullScreen
,但这会使应用程序进入全屏模式并覆盖Windows任务栏。
有没有一种方法(使用C#或XAML),即使在桌面上先前调整大小并关闭了UWP应用程序窗口,我也可以将其最大化?
我已经尝试使用ApplicationViewWindowingMode.FullScreen
,但这会使应用程序进入全屏模式并覆盖Windows任务栏。
您可以使用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;
}
}
如果你想在发布时最大化你的应用程序,你可以使用以下方法:
ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.Maximized;
但一定要将其放入页面的加载事件中,否则它将无法工作!
App.xaml.cs
下的 OnLaunched
事件中,对我来说起作用了。ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.FullScreen;
注意:请确保在以下行之前添加它
Window.Current.Activate();
ApplicationView.GetForCurrentView().TryEnterFullScreenMode();
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 });
}
我有一个一行代码,它的效果和Justin的代码一样,但是出于某种原因,当使用Justin的答案时,我的窗口无法最大化... 但是后来我改了一些东西,确实使其最大化了,但我失去了所有流畅设计,比如Acrylic和RevealHighlite...
所以我想出了这个一行代码,它能保持我所有的流畅设计原则都满意:
ApplicationView.GetForCurrentView().TryEnterFullScreenMode();
需要注意的一点:
我尝试了Justin的答案,并且在initializeComponent()之后直接调用了他的MaximizeWindowOnLoad()方法。
完整概述:
public class()
{
this.InitializeComponent();
MaximizeWindowOnLoad();
}
private static void MaximizeWindowOnLoad()
{
ApplicationView.GetForCurrentView().TryEnterFullScreenMode();
}
App
构造函数之前就已经被创建了。(2)VisibleBounds
仅返回窗口的大小。(使用Microsoft.NETCore.UniversalWindowsPlatform
v6.0.1) - Steven JeurisVisibleBounds
返回的是“窗口(应用视图)的可见区域”,而不是屏幕大小,即不是“窗口可以有多大”。我尝试让它工作(也许将来会),到那时我要么编辑要么添加自己的答案,但现在我只是留下了一条评论提供额外信息(如果您认为这是正确的话,可以在您的答案中包含此信息)。 - Steven JeurisVisibleBounds
不再返回完整的窗口大小了。请参见我的更新答案。 - Justin XL