Class Preference
Definition
- Namespace:
- Tizen.Applications
- Assembly:
- Tizen.Applications.Preference.dll
The Preference class provides methods to store and retrieve application specific data/preferences. Preferences are stored in the form of key-value pairs. Key names must be text strings while values can be integers, doubles, strings, or booleans.
C#Copypublic static class Preference
- Inheritance
-
objectPreference
Properties
Declaration
C#Copypublic static IEnumerable<string> Keys { get; }
Property Value
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<T><string> | The list of keys. |
Examples
CopyPreference.Set("Option_enabled", true); Preference.Set("active_user", "Joe"); Preference.Set("default_volume", 10); Preference.Set("brightness", "0.6"); foreach(string key in Preference.Keys) { Console.WriteLine("key {0}", key); }
Methods
Declaration
C#Copypublic static bool Contains(string key)
Parameters
Type | Name | Description |
---|---|---|
string | key | The name of the key to check. |
Returns
Type | Description |
---|---|
bool | True if the key exists in the preference, otherwise false. |
Remarks
This method checks if the specified key exists in the preferences. It returns true if the key exists, and false if not. The key argument should be a valid parameter. An ArgumentException will be thrown if the key is invalid. Additionally, an IOException may be thrown if the method fails due to an internal input/output error.
Examples
CopyPreference.Set("active_user", "Joe"); bool exists = Preference.Contains("active_user"); if (exists) { string value = Preference.Get<istring>("active_user"); Console.WriteLine("user {0}", value); }
Exceptions
Type | Condition |
---|---|
System.ArgumentException | Thrown if the key is an invalid parameter. |
System.IO.IOException | Thrown when the method failed due to an internal I/O error. |
Get<T>(string)
Gets the value of a preference item with the specified key. Note that this is a generic method.
Declaration
C#Copypublic static T Get<T>(string key)
Parameters
Type | Name | Description |
---|---|---|
string | key | The key of the preference. |
Returns
Type | Description |
---|---|
T | The value of the preference item if it is of the specified generic type. |
Type Parameters
Name | Description |
---|---|
T | The generic type to return. |
Examples
Copybool exists = Preference.Contains("active_user"); if (exists) { string value = Preference.Get<string>("active_user"); Console.WriteLine("user {0}", value); }
Exceptions
Type | Condition |
---|---|
System.Collections.Generic.KeyNotFoundException | Thrown if the key is not found. |
System.ArgumentException | Thrown if the key is an invalid parameter. |
System.IO.IOException | Thrown when the method failed due to an internal I/O error. |
Declaration
C#Copypublic static WeakReference<Preference.EventContext> GetEventContext(string key)
Parameters
Type | Name | Description |
---|---|---|
string | key | The preference key. |
Returns
Type | Description |
---|---|
System.WeakReference<T><Preference.EventContext> | The event context of respective key. |
Examples
Copyprivate static void Preference_PreferenceChanged(object sender, PreferenceChangedEventArgs e) { Console.WriteLine("key {0}", e.Key); } Preference.EventContext context = null; Preference.GetEventContext("active_user").TryGetTarget(out context); if(context != null) { context.Changed += Preference_PreferenceChanged; } Preference.Set("active_user", "Poe"); Preference.GetEventContext("active_user").TryGetTarget(out context); if (context != null) { context.Changed -= Preference_PreferenceChanged; }
Exceptions
Type | Condition |
---|---|
System.Collections.Generic.KeyNotFoundException | Thrown if the key is not found. |
System.ArgumentException | Thrown if the key is invalid parameter. |
See Also
Declaration
C#Copypublic static void Remove(string key)
Parameters
Type | Name | Description |
---|---|---|
string | key | The key to remove. |
Examples
Copybool exists = Preference.Contains("active_user"); if (exists) { string value = Preference.Remove("active_user"); }
Exceptions
Type | Condition |
---|---|
System.Collections.Generic.KeyNotFoundException | Thrown if the key is not found. |
System.IO.IOException | Thrown when the method failed due to an internal I/O error. |
Declaration
C#Copypublic static void RemoveAll()
Examples
CopyPreference.Set("Option_enabled", true); Preference.Set("active_user", "Joe"); Preference.Set("default_volume", 10); Preference.Set("brightness", "0.6"); Preference.RemoveAll();
Exceptions
Type | Condition |
---|---|
System.IO.IOException | Thrown when the method failed due to an internal I/O error. |
Declaration
C#Copypublic static void Set(string key, object value)
Parameters
Type | Name | Description |
---|---|---|
string | key | The name of the key to create/modify. |
object | value | The value corresponding to the key. |
Remarks
If the key already exists in the preference, the old value will be overwritten with a new value. Supported value data types include integers, doubles, strings, and booleans.
Examples
CopyPreference.Set("Option_enabled", true); Preference.Set("active_user", "Joe"); Preference.Set("default_volume", 10); Preference.Set("brightness", 0.6);
Exceptions
Type | Condition |
---|---|
System.ArgumentException | Thrown if the key is an invalid parameter. |
System.IO.IOException | Thrown when the method fails due to an internal I/O error. |