启动时最大化UWP应用程序窗口

Jes*_*ica 8 c# xaml windows-10 uwp uwp-xaml

有没有办法(C#或XAML)我可以最大化UWP应用程序窗口,即使我之前在桌面上调整大小并关闭它?

我尝试过,ApplicationViewWindowingMode.FullScreen但这使得应用程序全屏显示并覆盖了Widnows任务栏.

Jus*_* XL 9

您可以使用另一个值PreferredLaunchViewSizeApplicationViewWindowingMode,然后设置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)

  • @StevenJeuris你是什么意思?(2)?如果不知道窗户的大小,你会如何最大化它?关于(1),如果它在你的场景中不适合你,试着找出一种方法并改进答案,低估它对任何人都没有帮助. (2认同)