Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
448 views
in Technique[技术] by (71.8m points)

c# - How to ensure UWP app is always full screen on launch?

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?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

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.

Update

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.

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

The following is the updated code -

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...