Files
WCS/Cowain.Base/Helpers/GlobalData.cs
2026-03-02 09:08:20 +08:00

73 lines
1.8 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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;
}
}
}