Monodroid:我应该在哪里放置配置设置?

Jim*_* G. 8 configuration xamarin.android xamarin.mobile

来自Miguel de Icaza:

我们使用更适合移动设备的库配置文件,因此我们删除了不必要的功能(如整个System.Configuration堆栈,就像Silverlight一样).

经过多年的.NET开发的,我习惯了在存储配置设置web.configapp.config文件.

  • 使用Mono for Android时,我应该在哪里放置配置设置?
    • 如果重要,我还想为不同的构建配置存储不同的配置设置.

Mik*_*nis 12

我可能会建议使用共享首选项和编译符号来管理不同的配置.下面是如何使用首选项文件根据编译符号添加或更改键的示例.此外,您可以创建仅适用于特定配置的单独首选项文件.由于这些密钥在所有配置中都不可用,因此请确保在使用之前始终对它们执行检查.

var prefs = this.GetSharedPreferences("Config File Name", FileCreationMode.Private);
var editor = prefs.Edit();

#if MonoRelease
editor.PutString("MyKey", "My Release Value");
editor.PutString("ReleaseKey", "My Release Value");
#else
editor.PutString("MyKey", "My Debug Value");
editor.PutString("DebugKey", "My Debug Value");
#endif

editor.PutString("CommonKey", "Common Value");
editor.Commit();
Run Code Online (Sandbox Code Playgroud)