User Tools

Site Tools


notes:uwp:appsettings

Settings in UWP

Example: Store, read, and remove simple settings using ApplicationData.Current.LocalSettings:

using Windows.Storage;
...
ApplicationDataContainer settings = ApplicationData.Current.LocalSettings;
 
// Store settings.
settings.Values["LoginName"] = "Leon";
 
// Read settings.
if (settings.Values.ContainsKey("LoginName"))
{
    string str = (string)settings.Values["LoginName"];
    Debug.WriteLine(str);
}
 
// Remove settings.
settings.Values.Remove("LoginName");

Example: Store, read, and remove composite settings using ApplicationDataCompositeValue:

using Windows.Storage;
...
// Prepare settings.
ApplicationDataCompositeValue theme = new ApplicationDataCompositeValue();
theme["FontSize"] = 16;
theme.Add("FontBold", true); // you can also use the Add method
theme["Orientation"] = (int)Orientation.Horizontal; // only simple types can be stored
 
ApplicationDataContainer settings = ApplicationData.Current.LocalSettings;
 
// Store settings.
settings.Values["Theme"] = theme;
 
// Read settings.
ApplicationDataCompositeValue storedTheme = (ApplicationDataCompositeValue)settings.Values["Theme"];
 
if (storedTheme == null)
{
    Debug.WriteLine("No theme stored");
}
else
{
    if (storedTheme["FontSize"] != null)
        Debug.WriteLine((int)storedTheme["FontSize"]); // 16
 
    if (storedTheme["FontBold"] != null)
        Debug.WriteLine((bool)storedTheme["FontBold"]); // True
 
    if (storedTheme["Orientation"] != null)
        Debug.WriteLine((Orientation)storedTheme["Orientation"]); // Horizontal
}
 
// Remove a composite setting.
storedTheme.Remove("FontBold");

Example: Create a custom container to store settings using ApplicationDataContainer.CreateContainer:

using Windows.Storage;
...
ApplicationDataContainer settings = ApplicationData.Current.LocalSettings;
 
// ApplicationDataCreateDisposition.Always indicates that the data container should be created 
// only if it does not exist. If the container exists, a new container is not created. 
ApplicationDataContainer container = 
      settings.CreateContainer("LoginDataContainer", ApplicationDataCreateDisposition.Always);
if (settings.Containers.ContainsKey("LoginDataContainer"))
{
    settings.Containers["LoginDataContainer"].Values["LoginName"] = "Leon";
}
 
// Read settings from the container.
if (settings.Containers.ContainsKey("LoginDataContainer"))
{
    ApplicationDataContainer containerRead = settings.Containers["LoginDataContainer"];
    if (containerRead.Values.ContainsKey("LoginName"))
    {
        Debug.WriteLine(containerRead.Values["LoginName"]); // Leon
    }
}
 
// Delete the container.
settings.DeleteContainer("LoginDataContainer");

Example: Values of different types kept in LocalSettings:

public bool ValueBool
{
    get
    {
        ApplicationDataContainer settings = ApplicationData.Current.LocalSettings;
 
        if (settings.Values.ContainsKey("ValueBool"))
            return (bool)settings.Values["ValueBool"];
        else
            return default(bool);
    }
    set
    {
        ApplicationDataContainer settings = ApplicationData.Current.LocalSettings;
        settings.Values["ValueBool"] = value;
    }
}
 
public string ValueString
{
    get
    {
        ApplicationDataContainer settings = ApplicationData.Current.LocalSettings;
 
        if (settings.Values.ContainsKey("ValueString"))
            return (string)settings.Values["ValueString"];
        else
            return default(string);
    }
    set
    {
        ApplicationDataContainer settings = ApplicationData.Current.LocalSettings;
        settings.Values["ValueString"] = value;
    }
}
 
public int ValueInt
{
    get
    {
        ApplicationDataContainer settings = ApplicationData.Current.LocalSettings;
 
        if (settings.Values.ContainsKey("ValueInt"))
            return (int)settings.Values["ValueInt"];
        else
            return default(int);
    }
    set
    {
        ApplicationDataContainer settings = ApplicationData.Current.LocalSettings;
        settings.Values["ValueInt"] = value;
    }
}
 
public DateTime ValueDateTime
{
    get
    {
        ApplicationDataContainer settings = ApplicationData.Current.LocalSettings;
 
        if (settings.Values.ContainsKey("ValueDateTime"))
        {
            string dateStr = settings.Values["ValueDateTime"] as string;
            if (dateStr != null)
                return DateTime.ParseExact(dateStr, "yyyyMMddHHmmss",
                    System.Globalization.CultureInfo.InvariantCulture);
            else
                return default(DateTime);
        }
        else
            return default(DateTime);
    }
    set
    {
        ApplicationDataContainer settings = ApplicationData.Current.LocalSettings;
        settings.Values["ValueDateTime"] = value.ToString("yyyyMMddHHmmss");
    }
}
 
enum ViewTypeEnum
{
    Default,
    Full,
    Compact
}
 
public ViewTypeEnum ViewType
{
    get
    {
        ApplicationDataContainer settings = ApplicationData.Current.LocalSettings;
 
        if (settings.Values.ContainsKey("ViewType"))
        {
            object val = settings.Values["ViewType"];
            if (val == null)
                return ViewTypeEnum.Default;
 
            if (!(val is Int32))
                return ViewTypeEnum.Default;
 
            ViewTypeEnum viewType = (ViewTypeEnum)(Int32)val;
            return viewType;
        }
        else
            return ViewTypeEnum.Default;
    }
    set
    {
        ApplicationDataContainer settings = ApplicationData.Current.LocalSettings;
        settings.Values["ViewType"] = (int)value;
    }
}

The settings are stored in a registry hive file called Settings.dat. This is how to view the settings:

  • Launch RegEdit.exe and select HKEY_LOCAL_MACHINE.
  • Select from the menu: File > Load Hive
  • In the Load Hive dialog box, open the Settings.dat file. The Settings.dat file is located in the package folder %LocalAppData%\Packages\PackageFamilyName\Settings, for example C:\Users\wbs\AppData\Local\Packages\WBS.MyApp_j65jvz9r0x64t\Settings
  • Give the hive a key name.
  • Expand the HKEY_LOCAL_ MACHINE node.
  • Select the key name you just created. The data is shown as byte arrays.

Notes

  • You can store the following WinRT data types in LocalSettings: Boolean, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Single, Double, Char, String, DateTimeOffset, TimeSpan, Guid, Point, Size, and Rect as well as single-dimension arrays (max 8 KB) of these data types.
  • The Values property returns an ApplicationDataContainerSettings object, which implements the IObservableMap<String, Object> interface, which offers a MapChanged event that is raised whenever an item in the collection changes.
  • You can also roam settings by using ApplicationData.Current.RoamingSettings

Links

notes/uwp/appsettings.txt · Last modified: 2017/02/23 by admin