1
- using System . IO ;
1
+ using System ;
2
+ using System . IO ;
2
3
using System . Windows . Threading ;
3
4
using System . Threading . Tasks ;
4
5
using System . ComponentModel ;
5
6
using Microsoft . Win32 ;
6
- using System ;
7
7
using System . Windows . Input ;
8
+ using Microsoft . CodeAnalysis . Scripting ;
8
9
9
10
namespace UserInputMacro
10
11
{
@@ -13,8 +14,11 @@ class MainWindowViewModel : INotifyPropertyChanged
13
14
private ButtonState buttonState ;
14
15
private ScriptRecorder recorder ;
15
16
private string scriptPath ;
17
+ private string errorMessage ;
16
18
17
19
private static readonly PropertyChangedEventArgs scriptPathChangedEventArgs = new PropertyChangedEventArgs ( nameof ( ScriptPath ) ) ;
20
+ private static readonly PropertyChangedEventArgs errorMessageChangedEventArgs = new PropertyChangedEventArgs ( nameof ( ErrorMessage ) ) ;
21
+
18
22
public event PropertyChangedEventHandler PropertyChanged ;
19
23
20
24
public DelegateCommand RecordCommand { get ; set ; }
@@ -37,6 +41,19 @@ public string ScriptPath
37
41
}
38
42
}
39
43
44
+ public string ErrorMessage
45
+ {
46
+ get { return errorMessage ; }
47
+ set {
48
+ if ( errorMessage == value ) {
49
+ return ;
50
+ }
51
+
52
+ errorMessage = value ;
53
+ PropertyChanged ? . Invoke ( this , errorMessageChangedEventArgs ) ;
54
+ }
55
+ }
56
+
40
57
public MainWindowViewModel ( )
41
58
{
42
59
buttonState = new ButtonState ( ) ;
@@ -46,6 +63,8 @@ public MainWindowViewModel()
46
63
StopCommand = new DelegateCommand ( StopCmd_Execute , StopCmd_CanExecute ) ;
47
64
BrowseCommand = new DelegateCommand ( BrowseCmd_Execute ) ;
48
65
PlayCommand = new AsyncDelegateCommand ( PlayCmd_ExecuteAsync , PlayCmd_CanExecute ) ;
66
+
67
+ ErrorMessage = "[Message]" + Environment . NewLine + "If expected error occur, view to this text box." ;
49
68
}
50
69
51
70
private bool RecordCmd_CanExecute ( )
@@ -71,11 +90,27 @@ private void RecordCmd_Execute()
71
90
72
91
private async Task PlayCmd_ExecuteAsync ( )
73
92
{
74
- buttonState . IsPlaying = true ;
75
- await Task . Delay ( 50 ) ;
76
- WinDispacher ? . Invoke ( new Action ( CommandManager . InvalidateRequerySuggested ) ) ;
93
+ if ( ! File . Exists ( scriptPath ) ) {
94
+ ErrorMessage = "[File Error]" + Environment . NewLine + "'" + scriptPath + "' is not found." ;
95
+ return ;
96
+ }
97
+
98
+ ErrorMessage = "" ;
99
+
100
+ try {
101
+ buttonState . IsPlaying = true ;
102
+ await Task . Delay ( 50 ) ;
103
+ WinDispacher ? . Invoke ( new Action ( CommandManager . InvalidateRequerySuggested ) ) ;
104
+
105
+ await ScriptExecuter . ExecuteAsync ( ScriptPath ) ;
106
+ }
107
+ catch ( CompilationErrorException ex ) {
108
+ ErrorMessage = "[Compile Error]" + Environment . NewLine + ex . Message ;
109
+ }
110
+ catch ( Exception ) {
111
+ throw ;
112
+ }
77
113
78
- await ScriptExecuter . ExecuteAsync ( ScriptPath ) ;
79
114
buttonState . IsPlaying = false ;
80
115
}
81
116
0 commit comments