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