markus / MarkusAutoUpdate / src / NetSparkle.UI.WPF / Controls / BaseWindow.cs @ d8f5045e
이력 | 보기 | 이력해설 | 다운로드 (3.39 KB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.Text; |
4 |
using System.Threading; |
5 |
using System.Windows; |
6 |
|
7 |
namespace NetSparkleUpdater.UI.WPF.Controls |
8 |
{ |
9 |
/// <summary> |
10 |
/// Base <see cref="Window"/> class for most WPF update windows. |
11 |
/// Provides utilities and common code for windows that are shown. |
12 |
/// </summary> |
13 |
public class BaseWindow : Window |
14 |
{ |
15 |
/// <summary> |
16 |
/// Whether or not this Window is on the main thread or not |
17 |
/// </summary> |
18 |
protected bool _isOnMainThread; |
19 |
/// <summary> |
20 |
/// Whether or not the close window code has been called yet |
21 |
/// (so that it is not called more than one time). |
22 |
/// </summary> |
23 |
protected bool _hasInitiatedShutdown = false; |
24 |
|
25 |
/// <summary> |
26 |
/// Public, default construtor for BaseWindow objects |
27 |
/// </summary> |
28 |
public BaseWindow() |
29 |
{ |
30 |
|
31 |
} |
32 |
|
33 |
/// <summary> |
34 |
/// Create a BaseWindow and set up the closing event handler |
35 |
/// if requested |
36 |
/// </summary> |
37 |
/// <param name="useClosingEvent">true to use the <see cref="Window.Closing"/> |
38 |
/// event handler; false otherwise</param> |
39 |
public BaseWindow(bool useClosingEvent) |
40 |
{ |
41 |
if (useClosingEvent) |
42 |
{ |
43 |
Closing += BaseWindow_Closing; |
44 |
} |
45 |
} |
46 |
|
47 |
private void BaseWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e) |
48 |
{ |
49 |
Closing -= BaseWindow_Closing; |
50 |
if (!_isOnMainThread && !_hasInitiatedShutdown) |
51 |
{ |
52 |
_hasInitiatedShutdown = true; |
53 |
Dispatcher.InvokeShutdown(); |
54 |
} |
55 |
} |
56 |
|
57 |
/// <summary> |
58 |
/// Show this window to the user. |
59 |
/// </summary> |
60 |
/// <param name="isOnMainThread">true if the window is being shown while |
61 |
/// on the main thread; false otherwise</param> |
62 |
protected void ShowWindow(bool isOnMainThread) |
63 |
{ |
64 |
try |
65 |
{ |
66 |
Show(); |
67 |
_isOnMainThread = isOnMainThread; |
68 |
if (!isOnMainThread) |
69 |
{ |
70 |
// https://stackoverflow.com/questions/1111369/how-do-i-create-and-show-wpf-windows-on-separate-threads |
71 |
System.Windows.Threading.Dispatcher.Run(); |
72 |
} |
73 |
} |
74 |
catch (ThreadAbortException) |
75 |
{ |
76 |
Close(); |
77 |
if (!isOnMainThread) |
78 |
{ |
79 |
Dispatcher.InvokeShutdown(); |
80 |
} |
81 |
} |
82 |
} |
83 |
|
84 |
/// <summary> |
85 |
/// Close the window and shut down the UI dispatcher if necessary |
86 |
/// </summary> |
87 |
protected void CloseWindow() |
88 |
{ |
89 |
// make sure to close the window on the thread it has been started on |
90 |
Dispatcher.InvokeAsync(() => |
91 |
{ |
92 |
Close(); |
93 |
if (!_isOnMainThread && !_hasInitiatedShutdown) |
94 |
{ |
95 |
_hasInitiatedShutdown = true; |
96 |
Dispatcher.InvokeShutdown(); |
97 |
} |
98 |
}); |
99 |
} |
100 |
|
101 |
/// <summary> |
102 |
/// Bring this window to the front of all other windows by temporarily |
103 |
/// setting Topmost to true. |
104 |
/// </summary> |
105 |
protected void BringToFront() |
106 |
{ |
107 |
Topmost = true; |
108 |
Activate(); |
109 |
Topmost = false; |
110 |
} |
111 |
} |
112 |
} |