-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainViewModel.cs
83 lines (72 loc) · 2.93 KB
/
MainViewModel.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
using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Threading;
using DevExpress.Mvvm;
using DevExpress.Mvvm.DataAnnotations;
namespace SplashScreenManagerExample.ViewModels {
public class MainViewModel : ViewModelBase {
public ISplashScreenManagerService SplashScreenManagerService
{
get { return this.GetService<ISplashScreenManagerService>(); }
}
public IDispatcherService DispatcherService
{
get { return this.GetService<IDispatcherService>(); }
}
BackgroundWorker worker;
public bool CanStart() { return worker == null || !worker.IsBusy; }
[Command(CanExecuteMethodName = "CanStart")]
public void Start() {
if(this.SplashScreenManagerService != null) {
this.SplashScreenManagerService.ViewModel = new DXSplashScreenViewModel();
this.InitSplashScreenViewModel(this.SplashScreenManagerService.ViewModel);
this.SplashScreenManagerService.Show();
this.RunBackgroundWorker();
}
}
void RunBackgroundWorker() {
worker = new BackgroundWorker();
worker.DoWork += Worker_DoWork;
worker.WorkerSupportsCancellation = true;
worker.RunWorkerAsync();
}
private void Worker_DoWork(object sender, DoWorkEventArgs e) {
int i = -1;
while(++i < 100) {
if(worker.CancellationPending) {
e.Cancel = true;
break;
}
UpdateSplashScreenContent(i);
Thread.Sleep(200);
}
this.DispatcherService.Invoke(() => {
this.SplashScreenManagerService.Close();
worker.DoWork -= Worker_DoWork;
worker = null;
});
}
void UpdateSplashScreenContent(int progressValue) {
var state = string.Empty;
state = progressValue < 20 ? "Starting..." : progressValue < 70 ? "Loading data.." : "Finishing";
this.DispatcherService
.Invoke(() => {
this.SplashScreenManagerService.ViewModel.Progress = progressValue;
this.SplashScreenManagerService.ViewModel.Status = $"({progressValue} %) - {state}";
});
}
void InitSplashScreenViewModel(DXSplashScreenViewModel vm) {
vm.Title = "SOME BACKGROUND WORK";
vm.Subtitle = "This can take some time";
vm.Logo = new Uri("pack://application:,,,/logo.png");
vm.IsIndeterminate = false;
vm.Tag = new DelegateCommand(CancelOperation, CanCancelOperation);
}
public bool CanCancelOperation() { return worker != null && worker.IsBusy; }
public void CancelOperation() {
if(worker != null && worker.IsBusy)
worker.CancelAsync();
}
}
}