void NavigateWithWebResourceRequestCmdExecuted(object target, ExecutedRoutedEventArgs e) { // Prepare post data as UTF-8 byte array and convert it to stream // as required by the application/x-www-form-urlencoded Content-Type var dialog = new TextInputDialog( title: "NavigateWithWebResourceRequest", description: "Specify post data to submit to https://www.w3schools.com/action_page.php."); if (dialog.ShowDialog() == true) { string postDataString = "input=" + dialog.Input.Text; UTF8Encoding utfEncoding = new UTF8Encoding(); byte[] postData = utfEncoding.GetBytes( postDataString); MemoryStream postDataStream = new MemoryStream(postDataString.Length); postDataStream.Write(postData, 0, postData.Length); postDataStream.Seek(0, SeekOrigin.Begin); CoreWebView2WebResourceRequest webResourceRequest = WebViewEnvironment.CreateWebResourceRequest( "https://www.w3schools.com/action_page.php", "POST", postDataStream, "Content-Type: application/x-www-form-urlencoded\r\n"); webView.CoreWebView2.NavigateWithWebResourceRequest(webResourceRequest); } }
void SetUserAgentCmdExecuted(object target, ExecutedRoutedEventArgs e) { var dialog = new TextInputDialog( title: "SetUserAgent", description: "Enter UserAgent"); if (dialog.ShowDialog() == true) { WebViewSettings.UserAgent = dialog.Input.Text; } }
async void InjectScriptCmdExecuted(object target, ExecutedRoutedEventArgs e) { var dialog = new TextInputDialog( title: "Inject Script", description: "Enter some JavaScript to be executed in the context of this page.", defaultInput: "window.getComputedStyle(document.body).backgroundColor"); if (dialog.ShowDialog() == true) { string scriptResult = await webView.ExecuteScriptAsync(dialog.Input.Text); MessageBox.Show(this, scriptResult, "Script Result"); } }
void SetUserAgentCmdExecuted(object target, ExecutedRoutedEventArgs e) { if (_coreWebView2Settings == null) { _coreWebView2Settings = webView.CoreWebView2.Settings; } var dialog = new TextInputDialog( title: "SetUserAgent", description: "Enter UserAgent"); if (dialog.ShowDialog() == true) { _coreWebView2Settings.UserAgent = dialog.Input.Text; } }
void DownloadStartingCmdExecuted(object target, ExecutedRoutedEventArgs e) { try { webView.CoreWebView2.DownloadStarting += delegate( object sender, CoreWebView2DownloadStartingEventArgs args) { // Developer can obtain a deferral for the event so that the CoreWebView2 // doesn't examine the properties we set on the event args until // after the deferral completes asynchronously. CoreWebView2Deferral deferral = args.GetDeferral(); // We avoid potential reentrancy from running a message loop in the download // starting event handler by showing our download dialog later when we // complete the deferral asynchronously. System.Threading.SynchronizationContext.Current.Post((_) => { using (deferral) { // Hide the default download dialog. args.Handled = true; var dialog = new TextInputDialog( title: "Download Starting", description: "Enter new result file path or select OK to keep default path. Select cancel to cancel the download.", defaultInput: args.ResultFilePath); if (dialog.ShowDialog() == true) { args.ResultFilePath = dialog.Input.Text; UpdateProgress(args.DownloadOperation); } else { args.Cancel = true; } } }, null); }; webView.CoreWebView2.Navigate("https://demo.smartscreen.msft.net/"); } catch (NotImplementedException exception) { MessageBox.Show(this, "DownloadStarting Failed: " + exception.Message, "Download Starting"); } }