markus / MarkusAutoUpdate / src / NetSparkle.Samples.HandleEventsYourself / MainWindow.xaml.cs @ d8f5045e
이력 | 보기 | 이력해설 | 다운로드 (6.82 KB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.Linq; |
4 |
using System.Text; |
5 |
using System.Threading.Tasks; |
6 |
using System.Windows; |
7 |
using System.Windows.Controls; |
8 |
using System.Windows.Data; |
9 |
using System.Windows.Documents; |
10 |
using System.Windows.Input; |
11 |
using System.Windows.Media; |
12 |
using System.Windows.Media.Imaging; |
13 |
using System.Windows.Navigation; |
14 |
using System.Windows.Shapes; |
15 |
using System.Drawing; |
16 |
using System.Threading; |
17 |
using NetSparkleUpdater.Events; |
18 |
using NetSparkleUpdater.SignatureVerifiers; |
19 |
|
20 |
namespace NetSparkleUpdater.Samples.HandleEventsYourself |
21 |
{ |
22 |
/// <summary> |
23 |
/// Interaction logic for MainWindow.xaml |
24 |
/// </summary> |
25 |
public partial class MainWindow : Window |
26 |
{ |
27 |
private SparkleUpdater _sparkle; |
28 |
private UpdateInfo _updateInfo; |
29 |
private string _downloadPath = null; |
30 |
|
31 |
public MainWindow() |
32 |
{ |
33 |
InitializeComponent(); |
34 |
|
35 |
try |
36 |
{ |
37 |
Microsoft.Win32.Registry.CurrentUser.DeleteSubKeyTree("Software\\Microsoft\\NetSparkle.TestAppNetCoreWPF"); |
38 |
} |
39 |
catch { } |
40 |
|
41 |
// get sparkle ready |
42 |
DownloadUpdateButton.IsEnabled = false; |
43 |
InstallUpdateButton.IsEnabled = false; |
44 |
|
45 |
_sparkle = new SparkleUpdater("https://localhost:9902/appcast.xml", new Ed25519Checker(Enums.SecurityMode.Strict, null, "NetSparkle_Ed25519.pub")) |
46 |
{ |
47 |
UIFactory = null, |
48 |
}; |
49 |
// TLS 1.2 required by GitHub (https://developer.github.com/changes/2018-02-01-weak-crypto-removal-notice/) |
50 |
_sparkle.SecurityProtocolType = System.Net.SecurityProtocolType.Tls12; |
51 |
} |
52 |
|
53 |
private async void CheckUpdates_Click(object sender, RoutedEventArgs e) |
54 |
{ |
55 |
InstallUpdateButton.IsEnabled = false; |
56 |
UpdateInfo.Content = "Checking for updates..."; |
57 |
_updateInfo = await _sparkle.CheckForUpdatesQuietly(); |
58 |
// use _sparkle.CheckForUpdatesQuietly() if you don't want the user to know you are checking for updates! |
59 |
// if you use CheckForUpdatesAtUserRequest() and are using a UI, then handling things yourself is rather silly |
60 |
// as it will show a UI for things |
61 |
if (_updateInfo != null) |
62 |
{ |
63 |
switch (_updateInfo.Status) |
64 |
{ |
65 |
case Enums.UpdateStatus.UpdateAvailable: |
66 |
UpdateInfo.Content = "There's an update available!"; |
67 |
DownloadUpdateButton.IsEnabled = true; |
68 |
break; |
69 |
case Enums.UpdateStatus.UpdateNotAvailable: |
70 |
UpdateInfo.Content = "There's no update available :("; |
71 |
DownloadUpdateButton.IsEnabled = false; |
72 |
break; |
73 |
case Enums.UpdateStatus.UserSkipped: |
74 |
UpdateInfo.Content = "The user skipped this update!"; |
75 |
DownloadUpdateButton.IsEnabled = false; |
76 |
break; |
77 |
case Enums.UpdateStatus.CouldNotDetermine: |
78 |
UpdateInfo.Content = "We couldn't tell if there was an update..."; |
79 |
DownloadUpdateButton.IsEnabled = false; |
80 |
break; |
81 |
} |
82 |
} |
83 |
} |
84 |
|
85 |
private async void DownloadUpdate_Click(object sender, RoutedEventArgs e) |
86 |
{ |
87 |
// this is async so that it can grab the download file name from the server |
88 |
_sparkle.DownloadStarted -= _sparkle_StartedDownloading; |
89 |
_sparkle.DownloadStarted += _sparkle_StartedDownloading; |
90 |
|
91 |
_sparkle.DownloadFinished -= _sparkle_FinishedDownloading; |
92 |
_sparkle.DownloadFinished += _sparkle_FinishedDownloading; |
93 |
|
94 |
_sparkle.DownloadHadError -= _sparkle_DownloadError; |
95 |
_sparkle.DownloadHadError += _sparkle_DownloadError; |
96 |
|
97 |
_sparkle.DownloadMadeProgress += _sparkle_DownloadMadeProgress; |
98 |
|
99 |
await _sparkle.InitAndBeginDownload(_updateInfo.Updates.First()); |
100 |
// ok, the file is downloading now |
101 |
} |
102 |
|
103 |
private void _sparkle_DownloadMadeProgress(object sender, AppCastItem item, ItemDownloadProgressEventArgs e) |
104 |
{ |
105 |
DownloadInfo.Text = string.Format("The download made some progress! {0}% done.", e.ProgressPercentage); |
106 |
} |
107 |
|
108 |
private void _sparkle_DownloadError(AppCastItem item, string path, Exception exception) |
109 |
{ |
110 |
DownloadInfo.Text = "We had an error during the download process :( -- " + exception.Message; |
111 |
InstallUpdateButton.IsEnabled = false; |
112 |
} |
113 |
|
114 |
private void _sparkle_StartedDownloading(AppCastItem item, string path) |
115 |
{ |
116 |
DownloadInfo.Text = "Started downloading..."; |
117 |
InstallUpdateButton.IsEnabled = false; |
118 |
} |
119 |
|
120 |
private void _sparkle_FinishedDownloading(AppCastItem item, string path) |
121 |
{ |
122 |
DownloadInfo.Text = "Done downloading!"; |
123 |
InstallUpdateButton.IsEnabled = true; |
124 |
_downloadPath = path; |
125 |
} |
126 |
|
127 |
private void InstallUpdateButton_Click(object sender, RoutedEventArgs e) |
128 |
{ |
129 |
_sparkle.CloseApplication += _sparkle_CloseApplication; |
130 |
_sparkle.InstallUpdate(_updateInfo.Updates.First(), _downloadPath); |
131 |
} |
132 |
|
133 |
private void _sparkle_CloseApplication() |
134 |
{ |
135 |
System.Windows.Application.Current.Shutdown(); |
136 |
} |
137 |
|
138 |
private async void UpdateAutomaticallyButton_Click(object sender, RoutedEventArgs e) |
139 |
{ |
140 |
_sparkle.UserInteractionMode = Enums.UserInteractionMode.DownloadAndInstall; |
141 |
RunFullUpdateUpdateStatusLabel.Text = "Checking for update..."; |
142 |
_sparkle.UpdateDetected += _sparkle_FullUpdate_UpdateDetected; |
143 |
_sparkle.DownloadStarted += _sparkle_FullUpdate_StartedDownloading; |
144 |
_sparkle.DownloadFinished += _sparkle_FullUpdate_DownloadFileIsReady; |
145 |
_sparkle.CloseApplication += _sparkle_FullUpdate_CloseApplication; |
146 |
await _sparkle.CheckForUpdatesQuietly(); |
147 |
} |
148 |
|
149 |
private void _sparkle_FullUpdate_UpdateDetected(object sender, Events.UpdateDetectedEventArgs e) |
150 |
{ |
151 |
RunFullUpdateUpdateStatusLabel.Text = "Found update..."; |
152 |
} |
153 |
|
154 |
private void _sparkle_FullUpdate_StartedDownloading(AppCastItem item, string path) |
155 |
{ |
156 |
RunFullUpdateUpdateStatusLabel.Text = "Started downloading update..."; |
157 |
} |
158 |
|
159 |
private void _sparkle_FullUpdate_DownloadFileIsReady(AppCastItem item, string downloadPath) |
160 |
{ |
161 |
RunFullUpdateUpdateStatusLabel.Text = "Update is ready..."; |
162 |
} |
163 |
|
164 |
private async void _sparkle_FullUpdate_CloseApplication() |
165 |
{ |
166 |
RunFullUpdateUpdateStatusLabel.Text = "Closing application..."; |
167 |
await Task.Delay(2000); |
168 |
System.Windows.Application.Current.Shutdown(); |
169 |
} |
170 |
} |
171 |
} |