Bringing a modern WebView to your .NET WinForms and WPF Apps

Windows

Windows
One of the founding principles of Microsoft Edge is that it is evergreen, with automatic updates to Windows 10 continually refreshing both Microsoft Edge itself and web content throughout Windows 10. However, until recently, WinForms and WPF apps didn’t have access to the Microsoft Edge-powered WebView.

Earlier this week at Build 2018,
Please, Log in or Register to view URLs content!
, bringing the latest Microsoft Edge rendering engine to .NET WinForms and WPF apps for the first time in an upcoming release of Windows 10. In today’s post, we’ll provide more details on the benefits this brings to developers, and how you can preview this new capability in the
Please, Log in or Register to view URLs content!
.

A WebView for the modern web


In
Please, Log in or Register to view URLs content!
, we described the powerful experiences enabled by bringing a modern web platform to UWP, allowing developers to write HTML, CSS, and JS that seamlessly spans both browser as well as native app experiences.

As the growth of the web platform in general, and EdgeHTML in particular, have accelerated in recent releases, this has resulted in an increased performance and compatibility gap with the Trident-powered WebBrowser control, and many of our customers have asked for a way to incorporate the latest version of EdgeHTML into WinForms and WPF apps. We’re happy to address this feedback with a preview of the all-new EdgeHTML-powered
Please, Log in or Register to view URLs content!
, bringing the last three years of performance, reliability, and security enhancements to WinForms and WPF apps for the first time.

Getting started


Working with the
Please, Log in or Register to view URLs content!
and
Please, Log in or Register to view URLs content!
API may feel foreign to native .NET developers, so we’re building additional controls to simplify the experience and provide a more familiar environment. These controls wrap the
Please, Log in or Register to view URLs content!
to enable the control to feel more like a native .NET WinForms or WPF control, and provide a subset of the members from that class.

The WinForms and WPF controls are available today as a preview in the 3.0 release of the
Please, Log in or Register to view URLs content!
in the
Please, Log in or Register to view URLs content!
package. This means that upgrading from the Trident-powered WebBrowser control to the EdgeHTML-powered WebView in your WinForms or WPF app can be as easy as dragging in a new control from the toolbox.

Using WebView for WPF


Once you install the NuGet package, the WebView control appears in Windows Community Toolkit section of the Toolbox when the WPF Designer is open in Visual Studio or Blend.

Using the Designer


Drag the control from the Toolbox as you would any other control.

https://gist.github.com/kypflug/907e03e236958cc2b0c01a29ce27c12b

https://gist.github.com/kypflug/87ef801f19c3af6d66aa39fee8092419

Programmatically adding WebView


The WPF version of the control is in the Microsoft.Toolkit.Win32.UI.Controls.WPF namespace.

https://gist.github.com/kypflug/43fb419dde0e12100f8d9861be932bdc

https://gist.github.com/kypflug/ff50b3d10de389c2cd559b4b073d86c0

Using WebView for WinForms

Using the Designer


First, we’ll need to add a WinForms control from a NuGet package to the Toolbox in Visual Studio. In a future release, Visual Studio will do this automatically.

  • First, open the Visual Studio Toolbox, then right-click anywhere in the toolbox, and select the Choose Items
  • In the .NET Framework Components tab of the Choose Toolbox Items dialog box, click the Browse button to locate the Toolkit.Win32.UI.Controls.dll in your NuGet package folder.
    For help finding that folder, see
    Please, Log in or Register to view URLs content!
    .
  • After the DLL is added to the list of Toolbox controls, WebView is automatically
  • Close the Choose Toolbox Items dialog box.

The WebView control appears in the All Windows Forms section of the Toolbox when the Windows Forms Designer is open.

https://gist.github.com/kypflug/c43124825d1bc3a9622076b6e6dbb11e

Programmatically adding WebView


After installing the NuGet package, you can add the WebView to your application like any other control. The WinForms version of the control is in the Microsoft.Toolkit.Win32.UI.Controls.WinForms namespace.

https://gist.github.com/kypflug/14e0738c80940bbb80babd626de7eef4

We’re just getting started!


The current WinForms and WPF WebView implementations are an early preview, with some limitations when compared to the UWP WebView control. For the complete list of these limitations, see
Please, Log in or Register to view URLs content!
.

You can get started with WebView today using the Windows 10 April 2018 update or the latest Windows Insider Preview builds, where we’ll be adding more improvements as we continue towards a stable release. Please share your feedback or suggestions via
Please, Log in or Register to view URLs content!
on Twitter, in the
Please, Log in or Register to view URLs content!
project on GitHub, or in the comments below.

Happy Coding!

Please, Log in or Register to view URLs content!
, Senior Program Manager, Microsoft Edge
Please, Log in or Register to view URLs content!
, Principal Architect, Microsoft Edge

Please, Log in or Register to view URLs content!
 

Users who are viewing this thread

Top