Save & restore size of UWP AppWindow

315 Views Asked by At

I'm using the new class AppWindow to show secondary windows in my UWP app and would like to be able to save size and position and restore them when opening these secondary windows. I've tried to use LocalSettings to that end unsuccessfully:

// Create the window and set content
AppWindow appWindow = await AppWindow.TryCreateAsync();
appWindow.TitleBar.ExtendsContentIntoTitleBar = true;
appWindow.TitleBar.ButtonBackgroundColor = Colors.Transparent;
appWindow.TitleBar.ButtonForegroundColor = Colors.White;
Frame frame = new Frame();
frame.Navigate(pageType, item);
ElementCompositionPreview.SetAppWindowContent(appWindow, frame);
await appWindow.TryShowAsync();

// Get size from local settings (if saved)
var settingRect = string.Format("Window{0}", item.ItemType.Name);
if (LocalSettings.Values.ContainsKey(settingRect))
{
    var rect = ApplicationData.Current.LocalSettings.Values[settingRect].Convert<Rect>();
    appWindow.RequestSize(new Size(rect.Width, rect.Height));
}

appWindow.CloseRequested += delegate
{
    var placement = appWindow.GetPlacement();
    var rect = new Rect(placement.Offset, placement.Size);
    ApplicationData.Current.LocalSettings.Values[settingRect] = rect;

};

appWindow.Closed += delegate
{
    frame.Content = null;
    appWindow = null;
};

The method GetPlacement always returns 0 as width and height, no matter if it's called from the event CloseRequested or Closed. And if my main window is not open in maximized state, it takes the size and position of the last secondary window... :( Any idea how to proceed? I want each type of secondary window to appear at the same position with the same size.

1

There are 1 best solutions below

0
On

Looks like it was a capability issue. I applied a solution found here: App close confirmation in UWP

    <Package xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" IgnorableNamespaces="uap mp uap3">

<Capabilities>
    <rescap:Capability Name="confirmAppClose" />
</Capabilities>