【问题标题】:Save & restore size of UWP AppWindow保存和恢复 UWP AppWindow 的大小
【发布时间】:2020-06-15 20:30:46
【问题描述】:

我正在使用新类 AppWindow 在我的 UWP 应用程序中显示辅助窗口,并且希望能够在打开这些辅助窗口时保存大小和位置并恢复它们。为此,我尝试使用 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;
};

GetPlacement 方法总是返回 0 作为宽度和高度,无论它是从事件 CloseRequested 还是 Closed 中调用的。如果我的主窗口没有以最大化状态打开,它会占用最后一个辅助窗口的大小和位置...... :( 知道如何进行吗?我希望每种类型的辅助窗口以相同的大小出现在相同的位置。

【问题讨论】:

  • 我测试的时候可以触发CloseRequested事件,放置可以返回正确的宽高位置。所以我无法重现这个问题。你能提供一个简单的样本,可以复制给我们测试吗?

标签: c# uwp window uwp-appwindow


【解决方案1】:

看起来这是一个能力问题。我应用了在这里找到的解决方案: 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>

【讨论】:

    猜你喜欢
    • 2012-01-05
    • 2010-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多