简体   繁体   English

Properties.Settings.Default.Default.Save()不起作用

[英]Properties.Settings.Default.Default.Save() does not work

I have created code in WPF to let the window remember its last location like this: 我在WPF中创建了代码,让窗口记住它的最后位置,如下所示:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    try
    {
        Rect storedLoc = Properties.Settings.Default.WindowSavedLocation;
        this.Top = storedLoc.Top;
        this.Left = storedLoc.Left;
    }
    catch
    {
        MessageBox.Show("No settings stored !");
    }
}

private void Window_Closed(object sender, EventArgs e)
{
   Properties.Settings.Default.WindowSavedLocation = RestoreBounds;
   Properties.Settings.Default.Save();
}

When I build the application I can see that the app.exe.config file has the setting 当我构建应用程序时,我可以看到app.exe.config文件具有该设置

WindowSavedLocation

but it just does not save and throws no exception. 但它只是不保存,也不抛出异常。

Everytime I run the application, it says "No settings stored !". 每次我运行该应用程序时,都会显示“未存储任何设置!”。

It's scope is user. 它的范围是用户。

I repro. 我责备。 The Remarks section of the Window.RestoreBounds property docs is relevant to your problem: Window.RestoreBounds属性文档的“备注”部分与您的问题有关:

If you query RestoreBounds before the window has been shown or after it has been closed, Empty is returned. 如果在显示窗口之前或关闭窗口之后查询RestoreBounds,则返回Empty。

Use the Closing event instead so the RestoreBounds property is still valid. 请使用Closing事件,以使RestoreBounds属性仍然有效。

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

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