更新 WPF 桌面桥通用应用程序时无法升级设置 [英] Can't upgrade settings when updating a WPF Desktop Bridge Universal app
问题描述
我的应用程序是用 WPF C# 编写的,我直接从 Visual Studio 使用 MSIX 应用程序项目将其导出为通用应用程序.
My app is written in WPF C# and I export it as Universal app using MSIX Application Project straight from Visual Studio.
我只是无法让设置在更新之间保持不变.我在 MainWindow_Loaded
事件中使用以下代码:
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();
我尝试保持程序集信息版本相同,只是在 appx.manifest 中增加版本,但它不起作用.
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
我认为这可能与它不断生成新的哈希值有关,而不仅仅是将新版本作为子文件夹放置,这就是 Upgrade 不做任何事情的原因.
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.
到目前为止我发现的唯一信息是使用 Settings.Default.Upgrade()
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?
推荐答案
据我研究,这些设置不会使用桌面桥传输到 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.
这是代码,到目前为止我发现的唯一警告是您应该使用基本类型,因为这些方法将无法传输诸如 List
或 StringCollection
之类的东西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();
}
}
}
这篇关于更新 WPF 桌面桥通用应用程序时无法升级设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!