Class Preference
Definition
- Namespace:
- Tizen.Applications
- Assembly:
- Tizen.Applications.Preference.dll
- API Level:
- 3
The preference class provides APIs to store and retrieve an application specific data/preference. A preference is saved in the form of a key-value pair. Keys are always text strings and the value can be any one of the four types: integer, double, string, and boolean.
public static class Preference
- Inheritance
-
Preference
Properties
View SourceKeys
Retrieves all keys of the application preferences.
Declaration
public static IEnumerable<string> Keys { get; }
Property Value
Type | Description |
---|---|
IEnumerable<String> | The list of keys. |
Examples
Preference.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);
}
API Level: 3
Methods
View SourceContains(String)
Checks whether the given key exists in the preference.
Declaration
public static bool Contains(string key)
Parameters
Type | Name | Description |
---|---|---|
String | key | The name of the key to check. |
Returns
Type | Description |
---|---|
Boolean | True if the key exists in the preference, otherwise false. |
Examples
Preference.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 |
---|---|
ArgumentException | Thrown if the key is an invalid parameter. |
IOException | Thrown when the method failed due to an internal I/O error. |
API Level: 3
View SourceGet<T>(String)
Gets the value of a preference item with the specified key. Note that this is a generic method.
Declaration
public 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
bool exists = Preference.Contains("active_user");
if (exists)
{
string value = Preference.Get<string>("active_user");
Console.WriteLine("user {0}", value);
}
Exceptions
Type | Condition |
---|---|
KeyNotFoundException | Thrown if the key is not found. |
ArgumentException | Thrown if the key is an invalid parameter. |
IOException | Thrown when the method failed due to an internal I/O error. |
API Level: 3
View SourceGetEventContext(String)
Gets the event context for the given key.
Declaration
public static WeakReference<Preference.EventContext> GetEventContext(string key)
Parameters
Type | Name | Description |
---|---|---|
String | key | The preference key. |
Returns
Type | Description |
---|---|
WeakReference<Preference.EventContext> | The event context of respective key. |
Examples
private 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 |
---|---|
KeyNotFoundException | Thrown if the key is not found. |
ArgumentException | Thrown if the key is invalid parameter. |
See Also
API Level: 3
View SourceRemove(String)
Removes any preference value with the given key.
Declaration
public static void Remove(string key)
Parameters
Type | Name | Description |
---|---|---|
String | key | The key to remove. |
Examples
bool exists = Preference.Contains("active_user");
if (exists)
{
string value = Preference.Remove("active_user");
}
Exceptions
Type | Condition |
---|---|
KeyNotFoundException | Thrown if the key is not found. |
IOException | Thrown when the method failed due to an internal I/O error. |
API Level: 3
View SourceRemoveAll()
Removes all the key-value pairs from the preference.
Declaration
public static void RemoveAll()
Examples
Preference.Set("Option_enabled", true);
Preference.Set("active_user", "Joe");
Preference.Set("default_volume", 10);
Preference.Set("brightness", "0.6");
Preference.RemoveAll();
Exceptions
Type | Condition |
---|---|
IOException | Thrown when the method failed due to an internal I/O error. |
API Level: 3
View SourceSet(String, Object)
Sets a key-value pair representing the preference.
Declaration
public 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. Data types for supported values are: integer, double, string, and bool.
Examples
Preference.Set("Option_enabled", true);
Preference.Set("active_user", "Joe");
Preference.Set("default_volume", 10);
Preference.Set("brightness", "0.6");
Exceptions
Type | Condition |
---|---|
ArgumentException | Thrown if the key is an invalid parameter. |
IOException | Thrown when the method failed due to an internal I/O error. |