【问题标题】:Monodroid: Where should I put configuration settings?Monodroid:我应该把配置设置放在哪里?
【发布时间】:2013-01-18 14:28:04
【问题描述】:

来自Miguel de Icaza:

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

经过多年的 .NET 开发,我习惯将配置设置存储在 web.config 和 app.config 文件中。

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

【问题讨论】:

    标签: configuration xamarin.android xamarin.mobile


    【解决方案1】:

    我可能会建议使用共享首选项和编译符号来管理不同的配置。下面是一个示例,说明如何使用首选项文件根据编译符号添加或更改键。此外,您可以创建一个单独的首选项文件,该文件仅适用于特定配置。由于这些密钥并非在所有配置中都可用,因此请务必在使用前对其进行检查。

    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();
    

    【讨论】:

      【解决方案2】:

      我们在当前项目中遇到了完全相同的问题。 我的第一个冲动是将配置放在一个 sqlite 键值表中,但后来我的内部客户提醒我配置文件的主要原因 - 它应该支持简单的编辑。
      因此,我们创建了一个 XML 文件并将其放在那里:

      string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
      

      并使用这些属性访问它:

      public string this[string key]
          {
              get
              {
                  var document = XDocument.Load(ConfigurationFilePath);
                  var values = from n in document.Root.Elements()
                             where n.Name == key
                             select n.Value;
                  if(values.Any())
                  {
                      return values.First();
                  }
                  return null;
              }
              set
              {
                  var document = XDocument.Load(ConfigurationFilePath);
                  var values = from n in document.Root.Elements()
                             where n.Name == key
                             select n;
                  if(values.Any())
                  {
                      values.First().Value = value;
                  }
                  else
                  {
                      document.Root.Add(new XElement(key, value));
                  }
                  document.Save(ConfigurationFilePath);
              }
          }
      }
      

      通过我们称为 Configuration 的单例类,因此对于 .NET 开发人员来说,它与使用 app.config 文件非常相似。可能不是最有效的解决方案,但它可以完成工作。

      【讨论】:

        【解决方案3】:

        有一个以 Xamarin 为中心的 AppSetting 阅读器:https://www.nuget.org/packages/PCLAppConfig 对于持续交付非常有用(因此像 octopus 这样的部署服务器允许使用存储在 cd 服务器上的值来更改每个环境的配置文件)

        https://www.nuget.org/packages/PCLAppConfig 提供了一个以 Xamarin 为中心的 AppSetting 阅读器 它对于持续交付非常有用;

        按以下方式使用:

        1) 将 nuget 包引用添加到您的 pcl 和平台项目。

        2) 在您的 PCL 项目中添加一个 app.config 文件,然后作为链接文件添加到您的所有平台项目中。对于 android,确保将构建操作设置为“AndroidAsset”,对于 UWP,将构建操作设置为“内容”。添加设置键/值:<add key="config.text" value="hello from app.settings!" />

        3) 在您的每个平台项目上初始化 ConfigurationManager.AppSettings,就在“Xamarin.Forms.Forms.Init”语句之后,它位于 iOS 中的 AppDelegate、Android 中的 MainActivity.cs、UWP/Windows 8.1/ 中的 App 上WP 8.1:

        ConfigurationManager.Initialise(PCLAppConfig.FileSystemStream.PortableStream.Current);
        

        3) 阅读您的设置:ConfigurationManager.AppSettings["config.text"];

        【讨论】:

          【解决方案4】:

          ITNOA

          也许PCLAppConfig 可以帮助您在 Xamarin.Forms PCL 项目或其他 Xamarin 项目中创建和读取 app.config。

          对于不同构建模式下的不同配置,例如发布和调试,您可以在app.config上使用Configuration Transform。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-04-18
            • 2020-09-30
            • 2019-09-17
            • 1970-01-01
            • 2016-08-25
            • 2013-08-15
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多