启动时最大化UWP应用窗口 [英] Maximize UWP app window on launch

查看:126
本文介绍了启动时最大化UWP应用窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there a way (either C# or XAML) I can maximize a UWP app window even after I resized and closed it previously on desktop?

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

I have tried with ApplicationViewWindowingMode.FullScreen but this makes the app go entire full screen and covers the Widnows Taskbar too.

推荐答案

您可以使用 ApplicationViewWindowingMode 中的另一个值 PreferredLaunchViewSize ,然后设置 ApplicationView.PreferredLaunchViewSize ,但关键是找出 size 的大小.

You can use another value PreferredLaunchViewSize from ApplicationViewWindowingMode and then set ApplicationView.PreferredLaunchViewSize but the key is to find out what the size is going to be.

从理论上讲,您可以使用很大的数字,并且窗口会扩展到最大范围.但是,仅以有效像素为单位计算屏幕尺寸可能更安全.

Theoretically, you could use a really big number and window would just extend to the max it could be. However, it's probably safer to just calculate the screen dimensions in effective pixels.

因此,如果您只是在主 Page 上调用以下方法 before InitializeComponent(); ,它将在启动时最大化窗口.

So if you just call the following method before InitializeComponent(); on your main Page, it should maximize the window on startup.

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; 一次,然后删除所有代码.

Note the app somehow remembers these settings even after you uninstalled it. If you ever want to change back to the default behavior (app starts up with the previous window size), simply call ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.Auto; once and remove all the code.

类似于最新的Windows 10版本, ApplicationView.GetForCurrentView().VisibleBounds 不再以有效像素为单位返回 full 窗口大小.因此,我们现在需要一种新的计算方法.

Looks like in the latest Windows 10 build, ApplicationView.GetForCurrentView().VisibleBounds no longer returns the full window size in effective pixels anymore. So we now need a new way to calculate it.

事实证明,由于 DisplayInformation 类还为我们提供了屏幕分辨率以及比例因子.

Turns out it's quite straightforward since the DisplayInformation class also gives us the screen resolution as well as the scale factor.

以下是更新的代码-

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;
    }
} 

这篇关于启动时最大化UWP应用窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆