如何使用设置部署 uwp 应用程序

how deploy uwp app with settings

通常我会使用位于 app.config 中的预定义配置来发布我的应用程序。所以我可以为客户 A 发布其他预定义设置,然后为客户 B

如何在 uwp 中实现这一点?

由于 UWP 应用旨在通过商店部署,因此不再需要 app.config 文件。如果您仍想产生类似的行为,则必须使用编译器指令。

最简单的方法是创建一个类似于众所周知的文件 app.config:

<Config>
    <!-- key-value pairs go here -->
</Config>

为您拥有的每个客户创建一个配置文件...

  • Assets
    • CustomerA.config
    • CustomerB.config
    • CustomerC.config

...并通过编译器指令加载它:

public Config GetConfig()
{
    var configFileName = GetConfigName();
    // Load config...
}

private string GetConfigName()
{
#if CUSTA
    return "CustomerA.config";
#endif
#if CUSTB
    return "CustomerB.config";
#endif
#if CUSTC
    return "CustomerC.config";
#endif
    throw new NotSupportedException(
        "Assembly was compiled for a customer which config doesn't exist.");
}

注意:如果不同配置文件的数量可能会增加到一个未知数,您宁愿实施一个网络服务来识别客户并提供配置。

编辑: 也可以在您的应用程序中使用单个配置文件(这会阻止编译器指令),因此在不同的构建配置中通过 XSLT 修改 XML。
原理保持不变,但是您将代码中烦人的部分移到了 XSLT 中。

使用此设置 class https://github.com/joseangelmt/ObservableSettings。 有了它,你不需要创建一个 xml 文件,你可以通过你的属性的属性装饰来设置预定义的值,而且它是可观察的。