diff --git a/STranslate/Helper/ConfigHelper.cs b/STranslate/Helper/ConfigHelper.cs index 910c04e..852aee9 100644 --- a/STranslate/Helper/ConfigHelper.cs +++ b/STranslate/Helper/ConfigHelper.cs @@ -10,7 +10,7 @@ using System.Threading.Tasks; namespace STranslate.Helper { - public class ConfigHelper + public class ConfigHelper : Singleton { public T ReadConfig() { @@ -54,7 +54,5 @@ namespace STranslate.Helper /// private static string _ApplicationData => $"{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)}\\{_AppName}"; private static readonly string _AppName = Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location); - private static ConfigHelper _Instance; - public static ConfigHelper Instance { get => _Instance ?? (_Instance = new ConfigHelper()); } } } diff --git a/STranslate/Helper/MvvmHelper.cs b/STranslate/Helper/MvvmHelper.cs index 8919b8f..2400018 100644 --- a/STranslate/Helper/MvvmHelper.cs +++ b/STranslate/Helper/MvvmHelper.cs @@ -5,6 +5,18 @@ using System.Runtime.CompilerServices; namespace STranslate.Helper { + /// + /// Generate Singleton + /// + /// + public class Singleton where T : class + { + private static readonly Lazy _instance = new Lazy(() + => Activator.CreateInstance(typeof(T), true) as T, true); + + public static T Instance => _instance.Value; + } + /// /// 通知 /// diff --git a/STranslate/Helper/SqliteHelper.cs b/STranslate/Helper/SqliteHelper.cs index ae59ac0..b4ae1fe 100644 --- a/STranslate/Helper/SqliteHelper.cs +++ b/STranslate/Helper/SqliteHelper.cs @@ -9,6 +9,7 @@ using System.Threading.Tasks; namespace STranslate.Helper { + //https://github.com/praeclarum/sqlite-net public class SqliteHelper : IDisposable { public SqliteHelper() diff --git a/STranslate/ViewModel/MainVM.cs b/STranslate/ViewModel/MainVM.cs index d7662c4..53e0896 100644 --- a/STranslate/ViewModel/MainVM.cs +++ b/STranslate/ViewModel/MainVM.cs @@ -474,8 +474,8 @@ namespace STranslate.ViewModel /// public MainWindow Mainwin; - private static MainVM _instance; - public static MainVM Instance => _instance ?? (_instance = new MainVM()); + private static Lazy _instance = new Lazy(() => new MainVM()); + public static MainVM Instance => _instance.Value; public Hotkeys NHotkeys; private bool IsTopmost { get; set; } diff --git a/STranslate/ViewModel/SettingsVM.cs b/STranslate/ViewModel/SettingsVM.cs index ab8a920..0b183ef 100644 --- a/STranslate/ViewModel/SettingsVM.cs +++ b/STranslate/ViewModel/SettingsVM.cs @@ -110,8 +110,8 @@ namespace STranslate.ViewModel public ICommand EscCmd { get; private set; } - private static SettingsVM _instance; - public static SettingsVM Instance => _instance ?? (_instance = new SettingsVM()); + private static Lazy _instance = new Lazy(() => new SettingsVM()); + public static SettingsVM Instance => _instance.Value; /// /// 是否开机自启