Using API ConfigurationManager in Android project(Xamarin.Forms) throws an exception.

Singh Wong 51 Reputation points
2021-02-17T14:34:10.22+00:00

Steps:

  1. Add a configuration file in the Android project(app.config).
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
    <appSettings>
    <add key="key1" value="test1"/>
    <add key="key2" value="test2"/>
    </appSettings>
    </configuration>
  2. Install NuGet package in shared project:System.Configuration.ConfigutationManager 5.0.0
  3. In shared project, use API ConfigurationManager:
    public MainPage()
    {
    InitializeComponent();
    string str1 = ConfigurationManager.AppSettings["key1"];
    string str2 = ConfigurationManager.AppSettings["key2"];
    Debug.WriteLine(str1);
    Debug.WriteLine(str2);
    }
  4. Run the Android project and throw an exception:
    69077-%E6%88%AA%E5%B1%8F2021-02-17-%E4%B8%8B%E5%8D%88100208.png
Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. Cole Xia (Shanghai Wicresoft Co,.Ltd.) 6,756 Reputation points
    2021-02-18T04:44:57.35+00:00

    Hello,

    Welcome to Microsoft Q&A!

    It looks like ConfigurationManager is not supported on Xamarin.Android.

    We recommend you to use Xamarin.Essentials.Preferences instead.

    Usage

       //save  
       Preferences.Set("my_key", "my_value");  
         
       //retrieve  
       var myValue = Preferences.Get("my_key", "default_value");  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.