Friday 15 June 2012

monodroid - How do I use SharedPreferences in Xamarin.Android? -



monodroid - How do I use SharedPreferences in Xamarin.Android? -

i want save , retrieve application settings in xamarin.android project.

i know in android (java), utilize class sharedpreferences store information, not know how convert xamarin c#.

when type "sharedpreferences" xamarin studio ide, there no auto-completion, don't know use.

an initial search of interwebs took me related question, contains android java:

use shared preferences in xamarin

so summarise:

what xamarin android c# equivalent of android java's sharedpreferences?

the xamarin.android equivalent of sharedpreferences interface called isharedpreferences.

use in same way, , able port android code across.

for example, save true/false bool using context can following:

isharedpreferences prefs = preferencemanager.getdefaultsharedpreferences (mcontext); isharedpreferenceseditor editor = prefs.edit (); editor.putboolean ("key_for_my_bool_value", mbool); // editor.commit(); // applies changes synchronously on older apis editor.apply(); // applies changes asynchronously on newer apis

access saved values using:

isharedpreferences prefs = preferencemanager.getdefaultsharedpreferences (mcontext); mbool = prefs.getboolean ("key_for_my_bool_value"); mint = prefs.getint ("key_for_my_int_value"); mstring = prefs.getstring ("key_for_my_string_value");

from sample, can see 1 time know right c# interface use, rest easy. there many android java examples on how utilize sharedpreferences more complex situations, , these can ported using isharedpreferences.

for more information, read thread:

android shared preference on xamarin forum

android monodroid

No comments:

Post a Comment