【问题标题】:Windows Forms to UWP app via Desktop Bridge - User Settings are lost/reset on UWP app UpgradeWindows 窗体通过桌面桥到 UWP 应用程序 - UWP 应用程序升级时用户设置丢失/重置
【发布时间】:2020-10-13 22:34:05
【问题描述】:

我已使用桌面桥的最新版本(通过 Visual Studio 2017 中的打包项目)将我的 Windows 窗体(桌面)应用程序转换为 UWP,现在我正在测试我的应用程序的 UWP 安装和升级过程。当我的应用程序通过先前的方法(ClickOnce / .MSI / Windows Installer)部署时,我的用户设置(首选项)在我的应用程序升级到更高版本时被正确迁移和保留。在 UWP 下,所有用户设置在升级时都会丢失/重置。我的问题与此线程中描述的问题相同(未提供解决方案):

Here is the prior thread I found, same issue, no resolution

关于如何正确保留通过桌面桥带到 UWP 的桌面应用的用户设置的任何想法?

谢谢!

【问题讨论】:

  • 您的意思是从 MSI 升级到 UWP 时?或者从一个版本的 UWP 到更新版本的 UWP?
  • 我正在测试从应用程序的一个 UWP 版本升级到应用程序的更新 UWP 版本。就像我发布应用程序的更新版本时 MS Store 客户会做的那样。

标签: uwp desktop-bridge


【解决方案1】:

【讨论】:

  • 这是旧版/UWP 不兼容问题,有朝一日可以通过桌面桥或其他方法修复吗?如您所料,我的 WinForms 应用程序使用 My.Settings 来保持用户偏好,因为这是 WinForms 应用程序的标准。在测试我的应用程序的转换后的 UWP 版本时,它清楚地表明它能够毫无问题地将数据存储到 My.Settings 或从 My.Settings 检索数据。因此,目前在 UWP 中不起作用的唯一部分 My.Settings 是在应用升级时保留用户级 My.Settings 数据。是否有任何计划来提高该领域的旧版/UWP 兼容性?感谢您的回复!
  • @ElklandTechnologies 是的,我们正在寻找解决此问题的方法,以便应用不必更改其代码。这是我们积压的工作。现在,您必须更改代码并使用现代应用设置 API。
  • Stefan...我研究了“使用 UWP 扩展桌面应用程序”,但没有找到特定于从 WinForms 应用程序 (.NET) 中访问 ApplicationData.LocalSettings API 所需的最小更改的示例) 代码。从我收集到的信息看来,我需要向我的 WinForms 项目添加一个或多个引用来完成此操作,但我想知道您是否可以告诉我我需要添加的最少引用是多少。顺便说一句,我在 Nuget 包“Desktop Bridge Helpers”上找到了信息,这将帮助我编写条件代码以允许我的应用程序适应在旧版和 UWP 环境中运行。谢谢!
  • 添加对 Windows.winmd 和 System.Runtime.WindowsRuntime.dll 的引用。
  • Stefan...当我尝试添加对 Windows.winmd 的引用时,我的解决方案中出现了 2 个构建错误。我在这里添加了参考:docs.microsoft.com/en-us/windows/uwp/porting/…>。当我删除参考时,错误消失了。错误是: 1) “GenerateDesktopBridgeAppxManifest”任务没有为所需参数“EntryPointExe”指定值。 2) 生成清单的问题。无法加载文件或程序集 Windows.winmd 或其依赖项之一。试图加载格式不正确的程序。所以我无法添加参考。
【解决方案2】:

您可以将之前的 user.config 文件加载到当前设置中。这只是一种解决方法,可用于转换到 ApplicationData.LocalSettings。

public static void Init() {
    LoadPreviousSettings(ApplicationSettings.Default, MyFancySettings.Default);
}

private static void LoadPreviousSettings(params ApplicationSettingsBase[] applicationSettings)
{
    const string companyName = "YOUR_COMPANY_NAME_HERE";
    var userConfigXml = GetUserConfigXml(companyName);
    Configuration config = ConfigurationManager.OpenExeConfiguration(
        ConfigurationUserLevel.PerUserRoamingAndLocal);
    foreach (ApplicationSettingsBase setting in applicationSettings)
    {
        try
        {
            // loads settings from user.config into configuration
            LoadSettingSection(setting, config, userConfigXml);
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("userSettings");
        }
        catch (FileNotFoundException)
        {
            // Could not import settings.
        }
        setting.Reload();
    }

}

private static void LoadSettingSection(ApplicationSettingsBase setting, Configuration config, XDocument userConfigXml)
{
    string appSettingsXmlName = setting.ToString();
    var settings = userConfigXml.XPathSelectElements("//" + appSettingsXmlName);
    config.GetSectionGroup("userSettings")
        .Sections[appSettingsXmlName]
        .SectionInformation
        .SetRawXml(settings.Single().ToString());
}

private static XDocument GetUserConfigXml(string companyName)
{
    var localPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + $@"\{companyName}";
    // previous package folder
    var previousLocal = GetDirectoryByWriteTime(localPath, 1);
    // previous version, e.g. 1.2.0
    var prevousVersion = GetDirectoryByWriteTime(previousLocal, 0);
    // application settings for previous version
    return XDocument.Load(prevousVersion + @"\user.config");
}

private static string GetDirectoryByWriteTime(string path, int order)
{
    var direcotires = new DirectoryInfo(path).EnumerateDirectories()
        .OrderBy(d => d.LastWriteTime)
        .Reverse()
        .ToList();
    if (direcotires.Count > order)
    {
        var previous = direcotires[order];
        return previous.FullName;
    }
    throw new FileNotFoundException("Previous config file not found.");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-24
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    • 2022-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多