My goal is to have a singletin with my application shared data, preferences, etc. Want it to be singleton across my entire application and want it to be 2 way bindable in WPF using INotify.
I read that .net 4.5 can utilize StaticPropertyChanged
so I wonder if the below is how I would implement it as documentation seems spotty for this.
public static class Global
{
public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;
private static void CallPropChanged(string Prop)
{
StaticPropertyChanged?.Invoke(Settings, new PropertyChangedEventArgs(Prop));
}
private static Store Settings = new Store();
public static int setting1
{
get { return Settings._setting1; }
set { if (value != Settings._setting1) { Settings._setting1 = value; CallPropChanged("setting1"); } }
}
}
internal sealed class Store
{
internal int _setting1;
}
Am I on the right track for what I want to accomplish?
Edit: Made some modifications:
public sealed class Global:INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
static readonly Global _instance = new Global();
private int _setting1;
private int _setting2;
private void CallPropChanged(string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
field = value;
CallPropChanged(propertyName);
return true;
}
private Global() { }
public static int setting1
{
get { return _instance._setting1; }
set { _instance.SetField(ref _instance._setting1, value); }
}
public static int setting2
{
get { return _instance._setting2; }
set { _instance.SetField(ref _instance._setting2, value); }
}
}