| 1 | using Core; |
| 2 | using Core.Librarys; |
| 3 | using Core.Librarys.SQLite; |
| 4 | using Core.Servicers.Instances; |
| 5 | using Core.Servicers.Interfaces; |
| 6 | using Microsoft.Extensions.DependencyInjection; |
| 7 | using System; |
| 8 | using System.Collections.Generic; |
| 9 | using System.Configuration; |
| 10 | using System.Data; |
| 11 | using System.Diagnostics; |
| 12 | using System.Drawing; |
| 13 | using System.IO; |
| 14 | using System.Linq; |
| 15 | using System.Threading.Tasks; |
| 16 | using System.Windows; |
| 17 | using System.Windows.Controls; |
| 18 | using System.Windows.Resources; |
| 19 | using UI.Controls.Window; |
| 20 | using UI.Servicers; |
| 21 | using UI.ViewModels; |
| 22 | using UI.Views; |
| 23 | |
| 24 | namespace UI |
| 25 | { |
| 26 | ///<summary> |
| 27 | /// |
| 28 | ///</summary> |
| 29 | public partial class App : Application |
| 30 | { |
| 31 | private readonly ServiceProvider serviceProvider; |
| 32 | private System.Threading.Mutex mutex; |
| 33 | |
| 34 | private HideWindow keepaliveWindow; |
| 35 | |
| 36 | public App() |
| 37 | { |
| 38 | DispatcherUnhandledException += App_DispatcherUnhandledException; |
| 39 | |
| 40 | #if DEBUG |
| 41 | DispatcherUnhandledException -= App_DispatcherUnhandledException; |
| 42 | |
| 43 | #endif |
| 44 | |
| 45 | var serviceCollection = new ServiceCollection(); |
| 46 | ConfigureServices(serviceCollection); |
| 47 | serviceProvider = serviceCollection.BuildServiceProvider(); |
| 48 | } |
| 49 | |
| 50 | protected override void OnExit(ExitEventArgs e) |
| 51 | { |
| 52 | base.OnExit(e); |
| 53 | Logger.Save(true); |
| 54 | } |
| 55 | private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) |
| 56 | { |
| 57 | e.Handled = true; |
| 58 | |
| 59 | |
| 60 | Logger.Error("[程序崩溃异常] " + e.Exception.ToString()); |
| 61 | Logger.Save(true); |
| 62 | |
| 63 | |
| 64 | string taiBugPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, |
| 65 | "TaiBug.exe"); |
| 66 | ProcessHelper.Run(taiBugPath, new string[] { string.Empty }); |
| 67 | |
| 68 | |
| 69 | Shutdown(); |
| 70 | } |
| 71 | |
| 72 | #region 获取当前程序是否已运行 |
| 73 | ///<summary> |
| 74 | /// |
| 75 | ///</summary> |
| 76 | private bool IsRuned() |
| 77 | { |
| 78 | bool ret; |
| 79 | mutex = new System.Threading.Mutex(true, System.Reflection.Assembly.GetEntryAssembly().ManifestModule.Name, out ret); |
| 80 | if (!ret) |
| 81 | { |
| 82 | #if !DEBUG |
| 83 | return true; |
| 84 | |
| 85 | #endif |
| 86 | } |
| 87 | return false; |
| 88 | } |
| 89 | #endregion |
| 90 | |
| 91 | private void ConfigureServices(IServiceCollection services) |
| 92 | { |
| 93 | |
| 94 | services.AddSingleton<IDatabase, Database>(); |
| 95 | services.AddSingleton<IAppManager, AppManager>(); |
| 96 | services.AddSingleton<IWindowManager, WindowManager>(); |
| 97 | services.AddSingleton<IAppTimerServicer, AppTimerServicer>(); |
| 98 | services.AddSingleton<IAppObserver, AppObserver>(); |
| 99 | services.AddSingleton<IWebServer, WebServer>(); |
| 100 | services.AddSingleton<IMain, Main>(); |
| 101 | services.AddSingleton<IData, Data>(); |
| 102 | services.AddSingleton<IWebData, WebData>(); |
| 103 | services.AddSingleton<ISleepdiscover, Sleepdiscover>(); |
| 104 | services.AddSingleton<IAppConfig, AppConfig>(); |
| 105 | services.AddSingleton<IDateTimeObserver, DateTimeObserver>(); |
| 106 | services.AddSingleton<IAppData, AppData>(); |
| 107 | services.AddSingleton<ICategorys, Categorys>(); |
| 108 | services.AddSingleton<IWebFilter, WebFilter>(); |
| 109 | |
| 110 | |
| 111 | services.AddSingleton<IUIServicer, UIServicer>(); |
| 112 | services.AddSingleton<IAppContextMenuServicer, AppContextMenuServicer>(); |
| 113 | services.AddSingleton<IThemeServicer, ThemeServicer>(); |
| 114 | services.AddSingleton<IMainServicer, MainServicer>(); |
| 115 | services.AddSingleton<IInputServicer, InputServicer>(); |
| 116 | services.AddSingleton<IWebSiteContextMenuServicer, WebSiteContextMenuServicer>(); |
| 117 | services.AddSingleton<IStatusBarIconServicer, StatusBarIconServicer>(); |
| 118 | |
| 119 | |
| 120 | services.AddSingleton<MainViewModel>(); |
| 121 | |
| 122 | |
| 123 | services.AddTransient<IndexPage>(); |
| 124 | services.AddTransient<IndexPageVM>(); |
| 125 | |
| 126 | |
| 127 | services.AddTransient<DataPage>(); |
| 128 | services.AddTransient<DataPageVM>(); |
| 129 | |
| 130 | |
| 131 | services.AddTransient<SettingPage>(); |
| 132 | services.AddTransient<SettingPageVM>(); |
| 133 | |
| 134 | |
| 135 | services.AddTransient<DetailPage>(); |
| 136 | services.AddTransient<DetailPageVM>(); |
| 137 | |
| 138 | |
| 139 | services.AddTransient<CategoryPage>(); |
| 140 | services.AddTransient<CategoryPageVM>(); |
| 141 | |
| 142 | |
| 143 | services.AddTransient<CategoryAppListPage>(); |
| 144 | services.AddTransient<CategoryAppListPageVM>(); |
| 145 | |
| 146 | services.AddTransient<CategoryWebSiteListPage>(); |
| 147 | services.AddTransient<CategoryWebSiteListPageVM>(); |
| 148 | |
| 149 | services.AddTransient<ChartPage>(); |
| 150 | services.AddTransient<ChartPageVM>(); |
| 151 | |
| 152 | services.AddTransient<WebSiteDetailPage>(); |
| 153 | services.AddTransient<WebSiteDetailPageVM>(); |
| 154 | } |
| 155 | |
| 156 | private void OnStartup(object sender, StartupEventArgs e) |
| 157 | { |
| 158 | |
| 159 | if (IsRuned()) |
| 160 | { |
| 161 | Shutdown(); |
| 162 | } |
| 163 | |
| 164 | var main = serviceProvider.GetService<IMainServicer>(); |
| 165 | |
| 166 | bool isSelfStart = false; |
| 167 | if (e.Args.Length != 0) |
| 168 | { |
| 169 | if (e.Args[0].Equals("--selfStart")) |
| 170 | { |
| 171 | isSelfStart = true; |
| 172 | } |
| 173 | } |
| 174 | main.Start(isSelfStart); |
| 175 | |
| 176 | |
| 177 | keepaliveWindow = new HideWindow(); |
| 178 | } |
| 179 | } |
| 180 | } |