I\'m developing a Universal Windows Platform app but there is no Settings template in Visual Studio.
How can I implement an easy, strongly typed and observable class tha
Here an example of a settings class:
namespace Test
{
public class Settings : ObservableSettings
{
private static Settings settings = new Settings();
public static Settings Default
{
get { return settings; }
}
public Settings()
: base(ApplicationData.Current.LocalSettings)
{
}
[DefaultSettingValue(Value = 115200)]
public int Bauds
{
get { return Get(); }
set { Set(value); }
}
[DefaultSettingValue(Value = "Jose")]
public string Name
{
get { return Get(); }
set { Set(value); }
}
}
}
and here how to add an instance in your app.xaml:
Access and modify the values in a MVVM fashion:
Everything will be stored properly in your settings repository.
Here you have the implementation of DefaultSettingValue and ObservableSettings:
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Reflection;
using System.ComponentModel;
using Windows.Storage;
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public sealed class DefaultSettingValueAttribute : Attribute
{
public DefaultSettingValueAttribute()
{
}
public DefaultSettingValueAttribute(object value)
{
Value = value;
}
public object Value { get; set; }
}
public class ObservableSettings : INotifyPropertyChanged
{
private readonly ApplicationDataContainer settings;
public ObservableSettings(ApplicationDataContainer settings)
{
this.settings = settings;
}
public event PropertyChangedEventHandler PropertyChanged;
protected bool Set(T value, [CallerMemberName] string propertyName = null)
{
if (settings.Values.ContainsKey(propertyName))
{
var currentValue = (T)settings.Values[propertyName];
if (EqualityComparer.Default.Equals(currentValue, value))
return false;
}
settings.Values[propertyName] = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
return true;
}
protected T Get([CallerMemberName] string propertyName = null)
{
if (settings.Values.ContainsKey(propertyName))
return (T)settings.Values[propertyName];
var attributes = GetType().GetTypeInfo().GetDeclaredProperty(propertyName).CustomAttributes.Where(ca => ca.AttributeType == typeof(DefaultSettingValueAttribute)).ToList();
if (attributes.Count == 1)
return (T)attributes[0].NamedArguments[0].TypedValue.Value;
return default(T);
}
You can download a solution with a functional example from the repository I created in GitHub.