通常,我使用位于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.");
}
注意:如果不同的配置文件的数量可能增加到未知数,则您宁愿实施Web服务来识别客户并提供配置。
编辑:在您的应用程序中使用单个配置文件也是可能的(这将阻止编译器指令),因此可以通过XSLT在不同的构建配置中修改XML。
原理保持不变,但是您将令人讨厌的部分从代码导入XSLT中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句