-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.xaml.cs
188 lines (158 loc) · 5.18 KB
/
App.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using Application = System.Windows.Application;
using CustomToolbox.Common.Models;
using CustomToolbox.Common.Utils;
using MessageBox = System.Windows.MessageBox;
using Microsoft.Extensions.DependencyInjection;
using ServiceCollection = Microsoft.Extensions.DependencyInjection.ServiceCollection;
using System.Windows;
using System.Windows.Threading;
using CustomToolbox.Common.Extensions;
namespace CustomToolbox;
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
/// <summary>
/// IServiceProvider
/// </summary>
private IServiceProvider? ServiceProvider { get; set; }
/// <summary>
/// 應用程式預設的 LangData
/// </summary>
private static readonly LangData? _DefaultLangData = AppLangUtil.GetAppDefaultLangData();
/// <summary>
/// 應用程式語系資料
/// </summary>
private static readonly AppLangData _AppLangData = AppLangUtil.GetAppLangData();
/// <summary>
/// 預設的應用程式名稱
/// </summary>
private static readonly string DefaultTitle = "CustomToolbox";
public App()
{
// 參考來源:https://executecommands.com/dependency-injection-in-wpf-net-core-csharp/
ServiceCollection serviceCollection = new();
ConfigureServices(serviceCollection);
ServiceProvider = serviceCollection.BuildServiceProvider();
}
/// <summary>
/// 取得語系列表
/// </summary>
/// <returns>List<LangData></returns>
public static List<LangData>? GetLangData()
{
return _AppLangData.LangDatas;
}
/// <summary>
/// 應用程式預設的 LangData
/// </summary>
/// <returns>LangData</returns>
public static LangData? GetDefaultLangData()
{
return _DefaultLangData;
}
/// <summary>
/// 設定服務
/// </summary>
/// <param name="services">ServiceCollection</param>
private static void ConfigureServices(ServiceCollection services)
{
services.AddHttpClient().AddSingleton<WMain>();
}
/// <summary>
/// 啟動
/// </summary>
/// <param name="sender">StartupEventArgs</param>
/// <param name="e">object</param>
private void OnStartup(object sender, StartupEventArgs e)
{
try
{
InitUnhandledExceptionEventHandler();
CustomInit();
WMain? wMain = ServiceProvider?.GetService<WMain>();
wMain?.Show();
}
catch (Exception ex)
{
// 使用 MessageBox 輸出錯誤訊息。
MessageBox.Show(ex.GetExceptionMessage(),
caption: DefaultTitle,
button: MessageBoxButton.OK,
icon: MessageBoxImage.Error);
}
}
/// <summary>
/// 初始化 UnhandledException 的 EventHandler
/// <para>參考:https://blog.csdn.net/Iron_Ye/article/details/82913025</para>
/// </summary>
private static void InitUnhandledExceptionEventHandler()
{
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.UnhandledException += (object sender, UnhandledExceptionEventArgs args) =>
{
Exception ex = (Exception)args.ExceptionObject;
ShowErrorMsg(ex.GetExceptionMessage());
};
Current.DispatcherUnhandledException += (object sender, DispatcherUnhandledExceptionEventArgs e) =>
{
ShowErrorMsg(e.Exception.ToString());
e.Handled = true;
};
TaskScheduler.UnobservedTaskException += (object? sender, UnobservedTaskExceptionEventArgs e) =>
{
ShowErrorMsg(e.Exception.ToString());
e.SetObserved();
};
}
/// <summary>
/// 自定義初始化
/// </summary>
private static void CustomInit()
{
string errMsg = string.Empty;
// 更新設定值。
try
{
// 來源:https://stackoverflow.com/a/23924277
if (CustomToolbox.Properties.Settings.Default.UpdateSettings)
{
CustomToolbox.Properties.Settings.Default.Upgrade();
CustomToolbox.Properties.Settings.Default.Reload();
CustomToolbox.Properties.Settings.Default.UpdateSettings = false;
CustomToolbox.Properties.Settings.Default.Save();
}
}
catch (Exception ex)
{
errMsg += ex.GetExceptionMessage();
}
if (!string.IsNullOrEmpty(errMsg))
{
errMsg += Environment.NewLine;
}
errMsg += _AppLangData.ErrMsg;
if (!string.IsNullOrEmpty(errMsg))
{
errMsg += Environment.NewLine;
}
errMsg += AppLangUtil.SetAppLang();
ShowErrorMsg(errMsg);
}
/// <summary>
/// 顯示錯誤訊息
/// </summary>
/// <param name="message">字串,錯誤訊息</param>
private static void ShowErrorMsg(string message)
{
if (!string.IsNullOrEmpty(message))
{
// 使用 MessageBox 輸出錯誤訊息。
MessageBox.Show(message,
DefaultTitle,
MessageBoxButton.OK,
MessageBoxImage.Error);
}
}
}