Eels Fan
Eels Fan

Reputation: 2063

Using IsolatedStorage in Xamarin

I am attempting to build an application using Xamarin's Mono stuff (MonoTouch and MonoDroid). I am trying to write some code that will allow me to place content in local storage. Coming from a Windows Phone background, I thought this was the recommended approach in the Xamarin world also. On Windows Phone, I would have done the following:

IsolatedStorageSettings clientStorage = IsolatedStorageSettings.ApplicationSettings;
if (clientStorage != null)
{
  if (clientStorage.Contains("myKey"))
    clientStorage["myKey"] = value;
  else
    clientStorage.Add("myKey", value);
  clientStorage.Save();
}

While the Xamarin stack provides System.IO.IsolatedStorage, it does not provide the IsolatedStorageSettings class. Now, I feel stuck and I cannot find an example. My question is, how do I put a value in Isolated Storage in a Mono app?

Thank you

Upvotes: 2

Views: 7467

Answers (2)

Floyd Games
Floyd Games

Reputation: 1

The official mono project provides an implementation for IsolatedStorageSettings.

You can find it at: https://github.com/mono/moon/blob/master/class/System.Windows/System.IO.IsolatedStorage/IsolatedStorageSettings.cs.

Just add the file to your project and add "System.Runtime.Serialization" to your references.

The class creates a file "__LocalSettings" in the app directory and uses the DataContractSerializer to serialize your settings.

Upvotes: 0

Mike Stonis
Mike Stonis

Reputation: 2188

IsolatedStorageSettings is not implemented in the mobile solutions. It looks like it can be added, per that post, but it would have to be a custom implementation.

Generally though, you have a few other options.

  • Use the native supported mechanisms. Android: SharedPreferences (example). iOS: NSUserSettings (example).
  • Store settings in a serialized object using JSON or XML and then deserialize the object when you need access to it. There will probably a good quick and dirty option, but not recommended.
  • Use a SQLite database to store your settings. This would be a more cross platform option and probably the most recommended option.

Upvotes: 4

Related Questions