Szerkesztés

Megosztás a következőn keresztül:


Get started with WebView2 in WPF apps

This article covers how to set up your development tools and create an initial WebView2 app for Windows Presentation Foundation (WPF), and learn about WebView2 concepts along the way.

In this tutorial, you use the WPF Application or WPF App (.NET Framework) project template to create a WPF app, and then install the WebView2 SDK for the project to add WebView2.

Completed project

A completed version of this tutorial project is available in the WebView2Samples repo:

  • Sample name: WPF_GettingStarted
  • Repo directory: WPF_GettingStarted
  • Solution file: WPFSample.sln

Step 1 - Install Visual Studio with .NET support

This tutorial requires Microsoft Visual Studio, not Microsoft Visual Studio Code. This article mainly describes using Visual Studio 2022.

  1. Install Visual Studio. Install .NET desktop development support, to get the needed project templates, as follows.

  2. If you're at the Visual Studio startup screen, scroll to the bottom of the Create a new project dialog and click the link Open without code. Visual Studio opens.

  3. In Visual Studio, select Tools > Get Tools and Features. The Visual Studio Installer window opens, and the Modifying dialog opens over it.

  4. Select the .NET desktop development workload so there's a checkmark on it.

  5. In the section Installation details > .NET desktop development > Included on the right, make sure .NET desktop development tools and .NET Framework 4.7.2 development tools are listed, with a checkmark next to them.

  6. In the section Installation details > .NET desktop development > Optional on the right:

    • If you're using Visual Studio 2022, make sure Development tools for .NET is selected:

    Visual Studio 2022 'Modifying' dialog to install templates for '.NET desktop development'

    • If you're using Visual Studio 2019, make sure .NET development tools is selected:

    Visual Studio 2019 'Modifying' dialog to install templates for '.NET desktop development'

  7. Click the Modify button.

This tutorial also works with Visual Studio 2017. See Visual Studio older downloads. Install .NET support, to get the needed project templates, similar to the above steps.

Step 2 - Create a single-window WebView2 app

Start by creating a basic desktop project that contains a single main window.

  1. Decide whether to create a .NET Core/5/6 project (newer), or a WPF App (.NET Framework) project (older). For more information, see:

  2. Follow the applicable section below.

Creating a .NET Core/5/6 project

If you are creating a .NET Core/5/6 project, do the following steps. Otherwise, skip to Creating a WPF App (.NET Framework) project.

  1. Open Microsoft Visual Studio, such as Visual Studio 2022.

  2. In the opening panel, click Create new project. Or, in the main Visual Studio window, select File > New > Project. The Create a new project dialog opens.

  3. In the Search for templates text box, type WPF Application. The Create a new project panel shows the installed project templates that match your entered text. This article shows the C# rather than VB dialogs; both languages are supported for WebView2.

  4. If you're using Visual Studio 2022, click a project template that has the title WPF Application and the description text A project for creating a .NET WPF Application:

    Selecting the template 'WPF Application: .NET Core WPF Application' in the 2022 'Create a new project' dialog

    If you're using Visual Studio 2019, click a project template that has the title WPF Application and the description text A project for creating a .NET Core WPF Application:

    Selecting the template 'WPF Application: .NET Core WPF Application' in the 2019 'Create a new project' dialog

    If the above project template isn't listed, see Step 1 - Install Visual Studio with .NET support above, to install .NET desktop development tools.

  5. Click the Next button.

    The Configure your new project: WPF Application dialog opens:

    The 'Configure your new project: WPF Application' dialog for a .NET Core/5/6 project

  6. In the Project name text box, enter a project name, such as MyWpfDotnetCoreWv2App.

  7. In the Location text box, select a path on your local drive, such as C:\Users\myusername\Documents\MyProjects, and then click the Next button.

    The Additional information dialog appears, with a Target Framework dropdown list:

    The 'Additional information' dialog with a 'Target Framework' dropdown list

  8. Select .NET Core 3.1 or later, such as .NET 6.0. (Don't select .NET Core 3.0.) Then click the Create button.

    The initial .NET Core WPF Application project opens in Visual Studio:

    Initial project in Visual Studio 2022 using .NET Core WPF Application template

Skip to Step 3 - Build and run the initial project without WebView2 below.

Creating a WPF App (.NET Framework) project

If you are creating a WPF App (.NET Framework) project, do the following steps. Otherwise, skip to Step 3 - Build and run the initial project without WebView2.

  1. Open Microsoft Visual Studio, such as Visual Studio 2022.

  2. In the opening panel, click Create new project. Or, in the main Visual Studio window, select File > New > Project. The Create a new project dialog opens.

  3. In the Search for templates text box, type WPF App. The Create a new project panel shows the installed project templates that match your entered text. This article shows the C# rather than VB dialogs; both languages are supported for WebView2.

  4. Click a project template that has the title WPF App (.NET Framework) and the description text Windows Presentation Foundation client application:

    Selecting the template 'WPF App' in the 2022 'Create a new project' dialog

    If the above project template isn't listed, see Step 1 - Install Visual Studio with .NET support above, to install .NET desktop development tools.

  5. Click the Next button.

    The Configure your new project: WPF App (.NET Framework) dialog opens:

    The 'Configure your new project: WPF App (.NET Framework)' dialog

  6. In the Project name text box, enter a project name, such as MyWpfDotnetFwkWv2App.

  7. In the Location text box, select a path on your local drive, such as C:\Users\myusername\Documents\MyProjects.

  8. In the Framework dropdown list, select .NET Framework 4.6.2 or later.

  9. Click the Create button.

    The initial WPF App (.NET Framework) project opens in Visual Studio:

    Initial project in Visual Studio 2022 using the WPF App (.NET Framework) template

Step 3 - Build and run the initial project without WebView2

  1. Select File > Save All to save the project.

  2. Press F5 to build and run the project.

    The project runs, and displays an empty window:

    Empty app window without WebView2

    You might need to install the selected .NET Framework version, as follows.

  3. If the app doesn't open, select Debug > Start Without Debugging.

    If you haven't installed the selected version of .NET Framework, you might get the following dialog: "This application could not be started. The application requires one of the following versions of the .NET Framework: .NETFramework,Version=v4.8.1 - Do you want to install this .NET Framework version now?"

  4. If you get such a dialog, go to Download .NET Framework and download and then install the needed version of the Developer Pack (not the Runtime). For example, download ndp481-devpack-enu.exe to C:\Users\username\Downloads, and then double-click the file to install it.

  5. If prompted, restart your computer:

    Restart to install .NET Framework

  6. Go to the downloaded file, such as ndp481-devpack-enu.exe in C:\Users\username\Downloads, and again double-click the downloaded file to install the .NET Framework Developer Pack. A Success dialog appears:

    Setup Successful for installing .NET Framework

  7. If prompted, restart the computer again.

  8. Open Visual Studio, and open the solution you created.

  9. Press F5 to run the initial app (shown above), not yet including the WebView2 SDK.

  10. Close the initial app.

Step 4 - Install the WebView2 SDK

In Visual Studio, use NuGet Package Manager to add the WebView2 SDK to the project, as follows:

  1. In Solution Explorer, right-click the project name (based on either the .NET (Core) or .NET Framework project template), and then select Manage NuGet Packages:

    The 'Manage NuGet packages' command on the right-click menu

  2. In the upper left, click the Browse tab. In the search bar, type Microsoft.Web.WebView2, then click the Microsoft.Web.WebView2 package.

    The NuGet package manager dialog displays search results, including a Microsoft.Web.WebView2 package. The dialog has a version number and Install button.

    NuGet package manager dialog displays the Microsoft.Web.WebView2 package

  3. Accept the default version, and then click the Install button.

  4. In the Preview Changes dialog, click the OK button.

  5. Select File > Save All to save the project.

  6. Press F5 to build and run the project.

    The project runs, and displays an empty window. This verifies that WebView2 is installed and working, although WebView2 has no content to display yet:

    Empty app window with WebView2 SDK

  7. Close the app.

Step 5 - Create a single WebView2 control

Add a WebView2 control to your app.

  1. In the MainWindow.xaml file, to add the WebView2 XAML namespace, insert the following line inside the <Window/> tag:

    xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
    
  2. Make sure the code in MainWindow.xaml looks like the following code:

    <Window x:Class="WPF_Getting_Started.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
          xmlns:local="clr-namespace:{YOUR PROJECT NAME}"
          xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
          mc:Ignorable="d"
          Title="MainWindow"
          Height="450"
          Width="800"
    >
       <Grid>
    
       </Grid>
    </Window>
    
  3. To add the WebView2 control, replace the <Grid> tags with the following code. The Source property sets the initial URI displayed in the WebView2 control.

    <DockPanel>
       <wv2:WebView2 Name="webView"
                      Source="https://www.microsoft.com"
       />
    </DockPanel>
    
  4. Select File > Save All to save the project.

  5. Press F5 to build and run the project.

  6. Make sure your WebView2 control displays https://www.microsoft.com:

    The WebView2 control, displaying webpage content from microsoft.com

Step 6 - Navigation

Enable users to change the URL that the WebView2 control displays, by adding an address bar to the app.

  1. In the MainWindow.xaml file, add an address bar by copying and pasting the following code inside the <DockPanel> that contains the WebView2 control. Keep the existing code below the new snippet.

    <DockPanel DockPanel.Dock="Top">
        <Button x:Name="ButtonGo"
                  DockPanel.Dock="Right"
                  Click="ButtonGo_Click"
                  Content="Go"
        />
        <TextBox Name="addressBar"/>
    </DockPanel>
    
  2. Make sure the <DockPanel> section of the MainWindow.xaml file matches the following code:

    <DockPanel>
        <DockPanel DockPanel.Dock="Top">
            <Button x:Name="ButtonGo" DockPanel.Dock="Right" Click="ButtonGo_Click" Content="Go"/>
            <TextBox Name = "addressBar"/>
        </DockPanel>
        <wv2:WebView2 Name = "webView"
                      Source = "https://www.microsoft.com"
        />
    </DockPanel>
    
  3. In MainWindow.xaml.cs, to add the CoreWebView2 namespace, insert the following code at the top of the file:

    using Microsoft.Web.WebView2.Core;
    
  4. In the MainWindow.xaml.csfile, copy the following code to create the ButtonGo_Click method. This code navigates the WebView2 control to the URL entered in the address bar.

    private void ButtonGo_Click(object sender, RoutedEventArgs e)
    {
        if (webView != null && webView.CoreWebView2 != null)
        {
            webView.CoreWebView2.Navigate(addressBar.Text);
        }
    }
    
  5. Paste the code directly after the Public MainWIndow declaration, as shown in the following code:

    namespace WpfApp1
    {
       /// <summary>
       /// Interaction logic for MainWindow.xaml
       /// </summary>
       public partial class MainWindow : Window
       {
          public MainWindow()
          {
                InitializeComponent();
          }
          void ButtonGo_Click(object sender, RoutedEventArgs e)
          {
                if (webView != null && webView.CoreWebView2 != null)
                {
                   webView.CoreWebView2.Navigate(addressBar.Text);
                }
          }
       }
    }
    
  6. Select File > Save All to save the project.

  7. Press F5 to build and run the project.

  8. Type a new URL in the address bar and choose Go. For example, type https://www.bing.com.

  9. Make sure the WebView2 control opens the URL you entered.

    Make sure you enter a complete URL in the address bar. The app generates an ArgumentException if the URL doesn't start with http:// or https://.

    The sample app displays the Bing website with the URL https://www.bing.com in the address bar:

    The app displays the Bing website

Step 7 - Navigation events

During webpage navigation, the WebView2 control raises events. The app that hosts WebView2 controls listens for the following events:

  • NavigationStarting
  • SourceChanged
  • ContentLoading
  • HistoryChanged
  • NavigationCompleted

Navigation events, from new document, to navigation starting, through navigation completed

The above diagram shows the event sequence. Navigation events start with a new document.

Success path

A successful path includes the full sequence of events:

  1. Navigation starting.
  2. Source changed, with possible input from the same document.
  3. Content loading.
  4. History changes.
  5. Navigation completed.

For more information, see Navigation events for WebView2 apps.

Failure path

If theres a failure, the failure path proceeds directly from navigation starting, to navigation completed, skipping the intervening events.

When an error occurs, the following events are raised, and may depend on navigation to an error webpage:

  • SourceChanged
  • ContentLoading
  • HistoryChanged

Redirection

If an HTTP redirect occurs, there are multiple NavigationStarting events in a row.

Example demonstrating navigation events

To demonstrate how to use the events, register a handler for NavigationStarting that cancels any non-HTTPS requests, as follows.

  1. In the MainWindow.xaml.cs file, modify the constructor to match the top part of the following code. Below the constructor, add the EnsureHttps function:

    public MainWindow()
    {
        InitializeComponent();
        webView.NavigationStarting += EnsureHttps;
    }
    
    void EnsureHttps(object sender, CoreWebView2NavigationStartingEventArgs args)
    {
        String uri = args.Uri;
        if (!uri.StartsWith("https://"))
        {
            args.Cancel = true;
        }
    }
    

    In the constructor, EnsureHttps is registered as the event handler on the NavigationStarting event on the WebView2 control.

  2. Select File > Save All to save the project.

  3. Press F5 to build and run the project.

  4. Attempt to open an HTTP site. Make sure the WebView2 control remains unchanged.

  5. Attempt to open an HTTPS site. The WebView2 control allows you to open HTTPS sites.

Step 8 - Scripting

You can use host apps to inject JavaScript code into WebView2 controls at runtime. You can task WebView2 to run arbitrary JavaScript or add initialization scripts. The injected JavaScript applies to all new top-level documents and any child frames until the JavaScript is removed.

The injected JavaScript is run with specific timing:

  • Run it after the creation of the global object.
  • Run it before any other script included in the HTML document is run.

For example, add scripts that send an alert when a user navigates to non-HTTPS sites, as follows:

  1. Modify the EnsureHttps function to inject a script into the web content that uses ExecuteScriptAsync method.

    void EnsureHttps(object sender, CoreWebView2NavigationStartingEventArgs args)
    {
       String uri = args.Uri;
       if (!uri.StartsWith("https://"))
       {
          webView.CoreWebView2.ExecuteScriptAsync($"alert('{uri} is not safe, try an https link')");
          args.Cancel = true;
       }
    }
    
  2. Select File > Save All to save the project.

  3. Press F5 to build and run the project.

  4. Make sure the app displays an alert when you navigate to a website that doesn't use HTTPS.

    Message showing that an http: URL is not safe, and recommending trying an https: URL instead

Step 9 - Communication between host and web content

The host and web content can communicate in the following ways using postMessage:

  • Web content in a WebView2 control can post a message to the host using window.chrome.webview.postMessage. The host handles the message using any registered WebMessageReceived on the host.

  • Hosts post messages to web content in a WebView2 control using CoreWebView2.PostWebMessageAsString or CoreWebView2.PostWebMessageAsJSON. The messages are caught by handlers added to window.chrome.webview.addEventListener.

The communication mechanism passes messages from web content to the host using native capabilities.

In your project, when the WebView2 control navigates to a URL, it displays the URL in the address bar and alerts the user of the URL displayed in the WebView2 control.

  1. In MainWindow.xaml.cs, update your constructor and create an InitializeAsync function to match the following code. The InitializeAsync function awaits EnsureCoreWebView2Async, because the initialization of CoreWebView2 is asynchronous.

    public MainWindow()
    {
       InitializeComponent();
       webView.NavigationStarting += EnsureHttps;
       InitializeAsync();
    }
    
    async void InitializeAsync()
    {
       await webView.EnsureCoreWebView2Async(null);
    }
    
  2. After CoreWebView2 is initialized, register an event handler to respond to WebMessageReceived. In MainWindow.xaml.cs, update InitializeAsync and add UpdateAddressBar using the following code:

    async void InitializeAsync()
    {
       await webView.EnsureCoreWebView2Async(null);
       webView.CoreWebView2.WebMessageReceived += UpdateAddressBar;
    }
    
    void UpdateAddressBar(object sender, CoreWebView2WebMessageReceivedEventArgs args)
    {
       String uri = args.TryGetWebMessageAsString();
       addressBar.Text = uri;
       webView.CoreWebView2.PostWebMessageAsString(uri);
    }
    
  3. For the WebView2 control to send and respond to the web message, after CoreWebView2 is initialized, the host does the following:

    1. Injects a script to the web content that registers a handler to print message from the host.
    2. Injects a script to the web content that posts the URL to the host.
  4. In MainWindow.xaml.cs, update InitializeAsync to match the following code:

    async void InitializeAsync()
    {
       await webView.EnsureCoreWebView2Async(null);
       webView.CoreWebView2.WebMessageReceived += UpdateAddressBar;
    
       await webView.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync("window.chrome.webview.postMessage(window.document.URL);");
       await webView.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync("window.chrome.webview.addEventListener(\'message\', event => alert(event.data));");
    }
    
  5. Select File > Save All to save the project.

  6. Press F5 to build and run the project.

  7. When you open a new URI, the WebView2 control displays the URI in the address bar.

    The sample app displays the URI in the address bar and the Microsoft website, https://www.microsoft.com:

    The sample app displays the URI in the address bar and the Microsoft website

Congratulations, you built your first WebView2 app!

See also

developer.microsoft.com:

Local pages:

GitHub: