简体   繁体   English

保存和恢复 UWP AppWindow 的大小

[英]Save & restore size of UWP AppWindow

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'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:为此,我尝试使用 LocalSettings 失败:

// 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. GetPlacement 方法总是返回 0 作为宽度和高度,无论它是从事件 CloseRequested 还是 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. 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尺寸。

Looks like it was a capability issue.看起来这是一个能力问题。 I applied a solution found here: App close confirmation in UWP我应用了此处找到的解决方案: UWP 中的应用关闭确认

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM