Can't upgrade settings when updating a WPF Desktop Bridge Universal app

两盒软妹~` 提交于 2021-01-27 06:57:07

问题


My app is written in WPF C# and I export it as Universal app using MSIX Application Project straight from Visual Studio.

I just can't get the settings to persist between updates. I'm using the following code in the MainWindow_Loaded event:

Settings.Default.Upgrade();
Settings.Default.Save();
Settings.Default.Reload();

I tried keeping assembly information versions the same and just increment the version in the appx.manifest but it doesn't work.

I've noticed that each time the app updates it creates a new uniquely named parent settings folder (with a new hash every time) and the subfolder name is the version from the assembly. The folder structure is like this:

App.exe_Url_dfvfmfjs1qo33zsag1ay5w1s0rwg0u53/0.2.10.0/user.config

App.exe_Url_tazrvujdga5ujjarnahpkoscv5zbkgl0/0.2.10.0/user.config

I believe it might have to do with the fact that it keeps generating new hashes instead of just placing the new version as a subfolder and that's why Upgrade doesn't do anything.

The only information I've found so far is to use Settings.Default.Upgrade()

How am I supposed to transfer the old version settings to the new version when my universal desktop bridge app updates?


回答1:


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

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.

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();
            }
        }
    }


来源:https://stackoverflow.com/questions/61781890/cant-upgrade-settings-when-updating-a-wpf-desktop-bridge-universal-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!