Microsoft.Web.WebView2
> choose Microsoft.Web.WebView2.F5
. The running project displays an empty window.True
.(Name)
property to webView
.Source
property sets the initial URI displayed in the WebView2 control. Set the Source
property to https://www.microsoft.com
.F5
. Ensure your WebView2 control displays https://www.microsoft.com.addressBar
in the Properties Tab.Go!
and name the ButtongoButton
in the Properties Tab.Form1.cs
file, define Form_Resize
to keep the controls in place when the App Window is resized.F5
. Ensure the app displays similar to the following screenshot.F5
to build and run your project. Confirm that the app displays similar to the following screenshot.Form1.cs
file, to add the CoreWebView2
namespace, insert the following code snippet at the top.Form1.cs
add the CoreWebView2
namespace by inserting the following code snippet at the top of Form1.cs
.Go!
button to create the goButton_Click
method in the Form1.cs
file. Copy and paste the following snippet inside the function. Now, the goButton_Click
function navigates the WebView to the URL entered in the address bar.F5
. Enter a new URL in the address bar, and select Go. For example, enter https://www.bing.com
. Ensure the WebView2 control navigates to the URL.ArgumentException
is thrown if the URL does not start with http://
or https://
NavigationStarting
SourceChanged
ContentLoading
HistoryChanged
NavigationCompleted
SourceChanged
ContentLoading
HistoryChanged
NavigationStarting
events in a row.NavigationStarting
that cancels any requests not using HTTPS.Form1.cs
file, update the constructor to match the following code snippet and add the EnsureHttps
function.EnsureHttps
is registered as the event handler on the NavigationStarting
event on the WebView2 control.F5
. Ensure when navigating to an HTTP site, the WebView remains unchanged. However, the WebView will navigate to HTTPS sites.EnsureHttps
function to inject a script into the web content that uses ExecuteScriptAsync method.F5
. Ensure the app displays an alert when you navigate to a website that doesn't use HTTPS.postMessage
to communicate with each other as follows:window.chrome.webview.postMessage
to post a message to the host. The host handles the message using any registered WebMessageReceived
on the host.CoreWebView2.PostWebMessageAsString
or CoreWebView2.PostWebMessageAsJSON
. These messages are caught by handlers added to window.chrome.webview.addEventListener
.Form1.cs
file, update your constructor and create an InitializeAsync
function to match the following code snippet. The InitializeAsync
function awaits EnsureCoreWebView2Async because the initialization of CoreWebView2
is asynchronous.CoreWebView2
is initialized, register an event handler to respond to WebMessageReceived
. In the Form1.cs
file, update InitializeAsync
and add UpdateAddressBar
using the following code snippet.CoreWebView2
is initialized, the host injects a script in the web content to:postMessage
.Form1.cs
file, update InitializeAsync
to match the following code snippet.F5
. Now, the address bar displays the URI in the WebView2 control. Also, when you successfully navigate to a new URL, the WebView alerts the user of the URL displayed in the WebView.Version | Downloads | Last updated |
---|---|---|
8.0.1 | 46 | 4/8/2021 |
7.0.1 | 276 | 11/23/2020 |
6.0.1 | 182 | 10/12/2020 |
5.0.2 | 186 | 9/9/2020 |
5.0.1 | 159 | 9/7/2020 |
4.0.2 | 140 | 9/9/2020 |
4.0.1 | 186 | 8/7/2020 |
3.0.1 | 187 | 7/17/2020 |
2.0.4 | 204 | 6/14/2020 |
2.0.3 | 168 | 5/26/2020 |
1.0.3 | 171 | 4/30/2020 |
1.0.2 | 152 | 4/27/2020 |
1.0.1 | 156 | 4/27/2020 |
1.0.0 | 177 | 4/14/2020 |