Jes*_*ica 8 c# xaml windows-10 uwp uwp-xaml
有没有办法(C#或XAML)我可以最大化UWP应用程序窗口,即使我之前在桌面上调整大小并关闭它?
我尝试过,ApplicationViewWindowingMode.FullScreen但这使得应用程序全屏显示并覆盖了Widnows任务栏.
您可以使用另一个值PreferredLaunchViewSize从ApplicationViewWindowingMode,然后设置ApplicationView.PreferredLaunchViewSize,但关键是要找出的大小将是.
从理论上讲,你可以使用一个非常大的数字,窗口只会扩展到最大值.但是,以有效像素计算屏幕尺寸可能更安全.
因此,如果您在main 之前 调用以下方法,则应该在启动时最大化窗口.InitializeComponent();Page
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;
}
Run Code Online (Sandbox Code Playgroud)
请注意,即使您卸载它,应用程序也会以某种方式记住这些设置.如果您想要更改回默认行为(应用程序以前一个窗口大小启动),只需调用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;
}
}
Run Code Online (Sandbox Code Playgroud)