且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

更新 WPF 桌面桥通用应用程序时无法升级设置

更新时间:2023-11-20 15:03:28

据我研究,这些设置不会使用桌面桥传输到 UWP 更新.于是我开始使用 UWP 的原生 ApplicationData.Settings

As far as I researched these settings do not transfer to UWP updates using Desktop Bridge. So I started using UWP's native ApplicationData.Settings

作为一种解决方法,我创建了 2 种方法来使用 LocalSettings 更新新创建的 WPF 设置,这是 UWP 等效项,反之亦然.UWP 的 LocalSettings 传输更新.我在保存 WPF 设置时调用 Update(),在应用程序启动时调用 Load().它有效.

As a workaround I created 2 methods to update the newly created WPF settings using LocalSettings which is the UWP equivalent and vice versa. UWP's LocalSettings transfer on update. I call Update() when I save my WPF settings and I call Load() when the application starts. It works.

这是代码,到目前为止我发现的唯一警告是您应该使用基本类型,因为这些方法将无法传输诸如 ListStringCollection 之类的东西code>,为此我使用了序列化,尽管您也可以随时调整它们来执行此操作:

Here's the code, only caveat I've found so far is you should use the basic types as these methods will fail transferring something like a List<string> or a StringCollection, for that I'm using serialization, although you can always adapt them to do that too:

static class UWPSettings
    {
        public static void Update()
        {
            if (Startup.IsUniversalPlatform)
            {
                foreach (SettingsPropertyValue value in Properties.Settings.Default.PropertyValues)
                {
                    ApplicationData.Current.LocalSettings.Values[value.Name] = value.PropertyValue;
                }
            }
        }

        public static void Load()
        {
            if (Startup.IsUniversalPlatform)
            {
                foreach (var setting in ApplicationData.Current.LocalSettings.Values)
                {
                    foreach (SettingsPropertyValue s in Properties.Settings.Default.PropertyValues)
                    {
                        if (s.Name == setting.Key)
                        {
                            s.PropertyValue = setting.Value;
                        }
                    }
                }

                Properties.Settings.Default.Save();
            }
        }
    }