WebView2

WebView2 embeds Microsoft Edge web content inside a native app. Use it when web content is part of the product, not as a default replacement for native WinUI screens.

SDK Versus Runtime

PieceRole
WebView2 control/SDKAPI used by your WinUI app
Evergreen WebView2 RuntimeBrowser runtime installed and updated on user machines
Fixed Version RuntimeRuntime files shipped with your app for controlled compatibility/offline needs

Add A Control

<WebView2
    x:Name="Browser"
    Source="https://example.com" />

Initialize when you need direct CoreWebView2 access:

await Browser.EnsureCoreWebView2Async();
Browser.CoreWebView2.NavigationStarting += (_, args) =>
{
    if (!Uri.TryCreate(args.Uri, UriKind.Absolute, out var uri) || uri.Scheme != "https")
    {
        args.Cancel = true;
    }
};

Runtime Choice

ChoiceUse whenTrade-off
EvergreenMost appsAutomatic security and quality updates
Fixed VersionStrict compatibility, offline, regulated environmentLarger package and explicit update responsibility

Security Boundaries

  • Prefer HTTPS.
  • Restrict navigation to expected origins.
  • Treat host object/script injection as a privileged bridge.
  • Use a deliberate user data folder strategy for profiles, cache, and privacy.
  • Test web content with Playwright when it is complex enough to warrant browser automation.

Gotcha: The WinUI template may include the WebView2 SDK path, but users still need an appropriate WebView2 Runtime strategy.