1- using System . IO ;
1+ using System ;
2+ using System . IO ;
23using System . Windows . Threading ;
34using System . Threading . Tasks ;
45using System . ComponentModel ;
56using Microsoft . Win32 ;
6- using System ;
77using System . Windows . Input ;
8+ using Microsoft . CodeAnalysis . Scripting ;
89
910namespace UserInputMacro
1011{
@@ -13,8 +14,11 @@ class MainWindowViewModel : INotifyPropertyChanged
1314 private ButtonState buttonState ;
1415 private ScriptRecorder recorder ;
1516 private string scriptPath ;
17+ private string errorMessage ;
1618
1719 private static readonly PropertyChangedEventArgs scriptPathChangedEventArgs = new PropertyChangedEventArgs ( nameof ( ScriptPath ) ) ;
20+ private static readonly PropertyChangedEventArgs errorMessageChangedEventArgs = new PropertyChangedEventArgs ( nameof ( ErrorMessage ) ) ;
21+
1822 public event PropertyChangedEventHandler PropertyChanged ;
1923
2024 public DelegateCommand RecordCommand { get ; set ; }
@@ -37,6 +41,19 @@ public string ScriptPath
3741 }
3842 }
3943
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+
4057 public MainWindowViewModel ( )
4158 {
4259 buttonState = new ButtonState ( ) ;
@@ -46,6 +63,8 @@ public MainWindowViewModel()
4663 StopCommand = new DelegateCommand ( StopCmd_Execute , StopCmd_CanExecute ) ;
4764 BrowseCommand = new DelegateCommand ( BrowseCmd_Execute ) ;
4865 PlayCommand = new AsyncDelegateCommand ( PlayCmd_ExecuteAsync , PlayCmd_CanExecute ) ;
66+
67+ ErrorMessage = "[Message]" + Environment . NewLine + "If expected error occur, view to this text box." ;
4968 }
5069
5170 private bool RecordCmd_CanExecute ( )
@@ -71,11 +90,27 @@ private void RecordCmd_Execute()
7190
7291 private async Task PlayCmd_ExecuteAsync ( )
7392 {
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+ }
77113
78- await ScriptExecuter . ExecuteAsync ( ScriptPath ) ;
79114 buttonState . IsPlaying = false ;
80115 }
81116
0 commit comments