73 lines
1.8 KiB
C#
73 lines
1.8 KiB
C#
using Cowain.Base.Models;
|
||
using Cowain.Base.ViewModels;
|
||
using System.ComponentModel;
|
||
using System.Reflection;
|
||
using System.Runtime.CompilerServices;
|
||
|
||
namespace Cowain.Base.Helpers;
|
||
|
||
public class GlobalData : INotifyPropertyChanged
|
||
{
|
||
public event PropertyChangedEventHandler? PropertyChanged;
|
||
public void RaisePropertyChanged([CallerMemberName] string? propertyName = null)
|
||
{
|
||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||
}
|
||
private static GlobalData? _instance;
|
||
private static readonly object _lock = new object();
|
||
private GlobalData()
|
||
{
|
||
}
|
||
|
||
public static GlobalData Instance
|
||
{
|
||
get
|
||
{
|
||
if (_instance == null)
|
||
{
|
||
lock (_lock)
|
||
{
|
||
if (_instance == null)
|
||
{
|
||
_instance = new GlobalData();
|
||
}
|
||
}
|
||
}
|
||
return _instance;
|
||
}
|
||
}
|
||
|
||
private IDictionary<string, object> _activeDictionary = new Dictionary<string, object>();
|
||
|
||
public LoginUserViewModel? CurrentUser { get; set; }
|
||
public void AddOrUpdate(string key, object? value)
|
||
{
|
||
if (value is null)
|
||
{
|
||
return;
|
||
}
|
||
if (_activeDictionary.ContainsKey(key))
|
||
{
|
||
_activeDictionary[key] = value;
|
||
//字典中的值发生变化时,触发PropertyChanged事件
|
||
RaisePropertyChanged("Item");
|
||
}
|
||
else
|
||
{
|
||
_activeDictionary.Add(key, value);
|
||
// 添加新键时通知整个索引器
|
||
RaisePropertyChanged("Item[]");
|
||
}
|
||
}
|
||
|
||
|
||
public object? this[string key]
|
||
{
|
||
get
|
||
{
|
||
return _activeDictionary.TryGetValue(key, out var value) ? value : null;
|
||
}
|
||
|
||
}
|
||
}
|