Skip to content

Commit 1c4dcc1

Browse files
committed
#209. Added exception handling for the evaluate/execute JS stuff.
1 parent 85a014a commit 1c4dcc1

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

CefSharp.Wpf.Example/Views/Main/MainViewModel.cs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,27 @@ public MainViewModel()
7272

7373
private void EvaluateJavaScript(string s)
7474
{
75-
var result = webBrowser.EvaluateScript(s) ?? "null";
76-
MessageBox.Show("Result: " + result);
75+
try
76+
{
77+
var result = webBrowser.EvaluateScript(s) ?? "null";
78+
MessageBox.Show("Result: " + result);
79+
}
80+
catch (Exception e)
81+
{
82+
MessageBox.Show("Error while evaluating Javascript: " + e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
83+
}
7784
}
7885

7986
private void ExecuteJavaScript(string s)
8087
{
81-
webBrowser.ExecuteScriptAsync(s);
88+
try
89+
{
90+
webBrowser.ExecuteScriptAsync(s);
91+
}
92+
catch (Exception e)
93+
{
94+
MessageBox.Show("Error while executing Javascript: " + e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
95+
}
8296
}
8397

8498
private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
@@ -102,7 +116,7 @@ private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
102116
// TODO: This is a bit of a hack. It would be nicer/cleaner to give the webBrowser focus in the Go()
103117
// TODO: method, but it seems like "something" gets messed up (= doesn't work correctly) if we give it
104118
// TODO: focus "too early" in the loading process...
105-
WebBrowser.LoadCompleted += delegate { Application.Current.Dispatcher.BeginInvoke((Action) (() => webBrowser.Focus())); };
119+
WebBrowser.LoadCompleted += delegate { Application.Current.Dispatcher.BeginInvoke((Action)(() => webBrowser.Focus())); };
106120
}
107121

108122
break;
@@ -133,17 +147,6 @@ private void Go()
133147

134148
// Part of the Focus hack further described in the OnPropertyChanged() method...
135149
Keyboard.ClearFocus();
136-
137-
// Commented out, just want to be able to test EvaluateScript.
138-
try
139-
{
140-
var result = webBrowser.EvaluateScript("10 * 20;", null);
141-
var result2 = result;
142-
}
143-
catch (Exception e)
144-
{
145-
MessageBox.Show("Error while evaluating Javascript: " + e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
146-
}
147150
}
148151

149152
private void ViewSource()

0 commit comments

Comments
 (0)