public async Task LoadAsync(string url) { if (!IsReady()) { throw new InvalidOperationException(); } // navigate and handle LoadCompleted var navigationTcs = new TaskCompletionSource <bool>(); System.Windows.Navigation.LoadCompletedEventHandler handler = (s, e) => navigationTcs.TrySetResult(true); _browser.LoadCompleted += handler; try { _browser.Navigate(url); await navigationTcs.Task; } finally { _browser.LoadCompleted -= handler; } // make the content editable to check if WebBrowser shortcuts work well dynamic doc = _browser.Document; doc.body.firstChild.contentEditable = true; _events.FireLoaded(); }
async void MainWindow_Loaded(object sender, RoutedEventArgs eventArg) { // navigate the browser System.Windows.Navigation.LoadCompletedEventHandler loadedHandler = null; var loadedTcs = new TaskCompletionSource <bool>(); loadedHandler = (s, e) => { this.webBrowser.LoadCompleted -= loadedHandler; loadedTcs.SetResult(true); }; this.webBrowser.LoadCompleted += loadedHandler; this.webBrowser.Navigate("http://localhost:81/callback.html"); await loadedTcs.Task; // call the script method "scriptFuncAsync" var asyncScriptTcs = new TaskCompletionSource <object>(); var oncompleted = new ScriptEventHandler((ref object returnResult, object[] args) => { // we are here when the script has called us back asyncScriptTcs.SetResult(args[0]); }); this.webBrowser.InvokeScript("scriptFuncAsync", oncompleted); await asyncScriptTcs.Task; // show the result of the asyc call dynamic result = asyncScriptTcs.Task.Result; MessageBox.Show(result.outcome.ToString()); }