3

How can I maximize a window in a UWP project (not fullscreen!) using C#? I tried the method TryResizeView with the window bounds as parameters but nothing happens.

Thanks

WJM
  • 1,137
  • 1
  • 15
  • 30

3 Answers3

2

This is not possible in UWP at this time. So, I can't get you Maximize, but I can get you pretty close:

var av = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView();
var di = Windows.Graphics.Display.DisplayInformation.GetForCurrentView();
var bounds = av.VisibleBounds;
var factor = di.RawPixelsPerViewPixel;
var size = new Windows.Foundation.Size(bounds.Width * factor, bounds.Height * factor);
size.Height -= 100;
size.Width -= 100;
av.TryResizeView(size);

Basically, subtract 100 (to allow for the taskar). This will work in most cases, but without a true screen bounds measurement, I think this is the best we can do right now. Sorry I can't give you more. The TaskBar size (and location) is the variable.

This is implemented in http://aka.ms/template10 (https://github.com/Windows-XAML/Template10/blob/master/Template10%20(Library)/Utils/MonitorUtils.cs#L58)

Best of luck!

Jerry Nixon
  • 31,313
  • 14
  • 117
  • 233
  • If TryResizeView() does not work for you and you are content with, that the size is only setted at beginning, you can use this instead: `public MainPage() { this.InitializeComponent(); ApplicationView.PreferredLaunchViewSize = new Size(sizeX, sizeY); ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize; } ` – Matt126 Feb 07 '16 at 12:01
  • Thanks Jerry. What is the proper place to put this in as a feature request? – WJM Feb 07 '16 at 13:18
  • Add suggestions and feedback via [Windows developer feedback site on UserVoice](https://wpdev.uservoice.com/) – Neil Turner Feb 07 '16 at 14:11
  • @jerrynixonmsft: Where do I put this code from Template10 : MonitorUtils.Current().Maximize()? It gives me an ArgumentNullException regardless whether I put in App.xaml.cs (OnLaunched) or one of the Pages (e.g. OnNavigatedTo). – lukasz Jun 07 '16 at 07:12
  • I did an experiment with my UWP app and using the code below causes the app to be essentially maximized but it isn’t full screen and does not cover the taskbar. I do this at app launch: ApplicationView.PreferredLaunchViewSize = new Windows.Foundation.Size( 1000000, 1000000 ); ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize; I would assume that UWP apps can’t be sized larger than the available screen space. It’s not guaranteed to work but it does right now. Again, this worked for me but isn't a robust safe solution. – David Rector Jan 03 '17 at 01:12
2

Here's a solution that does not involve manually resizing. It does the same thing as clicking on the maximize button. And even if these resizing solutions used to work, they don't seem to now (running 1909).

The Windows system keyboard shortcuts to maximize and minimize the active window are WinKey + Up Arrow, WinKey + Down Arrow, respectively. Your UWP app can simulate keystrokes on itself using InputInjector.

First you must enable InputInjector as a restricted capability in your App Manifest.

<Package
    ...
    xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
    IgnorableNamespaces="... rescap">
...
<Capabilities>
    <rescap:Capability Name="inputInjectionBrokered"/>
</Capabilities>
</Package>

And then you can use something like this:

using Windows.UI.Input.Preview.Injection;

public static void MaximizeCurrentWindow()
{
    var inputInjector = InputInjector.TryCreate();

    var winKeyDown = new InjectedInputKeyboardInfo
    {
        VirtualKey = (ushort)VirtualKey.LeftWindows
    };

    var upArrowKeyDown = new InjectedInputKeyboardInfo
    {
        VirtualKey = (ushort)VirtualKey.Up
    };

    var winKeyUp = new InjectedInputKeyboardInfo
    {
        VirtualKey = (ushort)VirtualKey.LeftWindows,
        KeyOptions = InjectedInputKeyOptions.KeyUp
    };

    var upArrowKeyUp = new InjectedInputKeyboardInfo
    {
        VirtualKey = (ushort)VirtualKey.Up,
        KeyOptions = InjectedInputKeyOptions.KeyUp
    };

    InjectedInputKeyboardInfo[] macro = { winKeyDown, upArrowKeyDown, winKeyUp, upArrowKeyUp};
    inputInjector.InjectKeyboardInput(macro);
}
Sean O'Neil
  • 1,222
  • 12
  • 22
1

This answer didn't work for me. I modified this to be,,,,and now it works.

var av = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView();
        var di = Windows.Graphics.Display.DisplayInformation.GetForCurrentView();
        var bounds = av.VisibleBounds;
        var factor = di.RawPixelsPerViewPixel;
        Size size = new Size(di.ScreenWidthInRawPixels, di.ScreenHeightInRawPixels);
        size.Height -= 100;
        size.Width -= 100;
        av.TryResizeView(size);
John Leone
  • 273
  • 2
  • 9