如何在 WPF multi window 应用程序中动态加载和保存用户设置?

How to dynamicaly load and save user settings in WPF multi window application?

我正在尝试在 WPF 应用程序中保存用户设置。我想保存它们的 Window 位置和大小。为此,我激励自己 this.

我稍微更改了保存代码以执行以下操作:

 var oBaseProp = Properties.Settings.Default.Properties["baseProp"];
            Properties.Settings.Default["baseProp"] = "1";
            System.Configuration.SettingsProperty property = new System.Configuration.SettingsProperty("W_" + this.GetType().FullName + "_PLACEMENT"
                , typeof(string), oBaseProp.Provider, false, GetPlacement(this), System.Configuration.SettingsSerializeAs.String, oBaseProp.Attributes,
                false, false);
            Properties.Settings.Default.Properties.Add(property);
            Properties.Settings.Default["W_" + this.GetType().FullName + "_PLACEMENT"] = GetPlacement(this);
            Properties.Settings.Default.Save();

BaseProp 是在我的设置中定义的字符串 属性,它无用,仅用于检索提供程序和其他内容。

保存一切正常,我在 C:\Users\christophe.mom.DOMAINE-EPSILOG\AppData\Local\Epsilog\MSSante_GUI.vshost.exe_Url_fkwxpmo4exiyrjk2vha2qu2upx5sgrvn.0.0.0 路径中有一个新文件,user.config 包含所需数据。

问题是,我无法将此文件加载为设置文件:

 void StandardWindow_SourceInitialized(object sender, EventArgs e)
    {
        try
        {
            Properties.Settings.Default.Reload();
            var sPlacement = (string)Properties.Settings.Default.Properties["W_" + this.GetType().FullName + "_PLACEMENT"].DefaultValue;
            if (!string.IsNullOrWhiteSpace(sPlacement))
            {
                this.SetPlacement(this, sPlacement);
            }
        }
        catch { }
    }

我不明白为什么没有加载我的实际设置。我也尝试过执行(不是调试)模式,但仍然没有结果。

有人有想法吗?

谢谢!

好吧,事实上,这只适用于发布时编译的执行模式(在我的例子中)。