Share via


CoreWebView2.NavigationCompleted Event

Definition

NavigationCompleted is raised when the WebView has completely loaded (body.onload has been raised) or loading stopped with error.

public event EventHandler<Microsoft.Web.WebView2.Core.CoreWebView2NavigationCompletedEventArgs> NavigationCompleted;
member this.NavigationCompleted : EventHandler<Microsoft.Web.WebView2.Core.CoreWebView2NavigationCompletedEventArgs> 
Public Custom Event NavigationCompleted As EventHandler(Of CoreWebView2NavigationCompletedEventArgs) 

Event Type

Examples

void DOMContentLoadedCmdExecuted(object target, ExecutedRoutedEventArgs e)
{
    _iWebView2.CoreWebView2.DOMContentLoaded += WebView_DOMContentLoaded;
    _iWebView2.CoreWebView2.FrameCreated += WebView_FrameCreatedDOMContentLoaded;
    _iWebView2.NavigateToString(@"<!DOCTYPE html>" +
                              "<h1>DOMContentLoaded sample page</h1>" +
                              "<h2>The content to the iframe and below will be added after DOM content is loaded </h2>" +
                              "<iframe style='height: 200px; width: 100%;'/>");
    _iWebView2.CoreWebView2.NavigationCompleted += (sender, args) =>
    {
        _iWebView2.CoreWebView2.DOMContentLoaded -= WebView_DOMContentLoaded;
        _iWebView2.CoreWebView2.FrameCreated -= WebView_FrameCreatedDOMContentLoaded;
    };
}

void WebView_DOMContentLoaded(object sender, CoreWebView2DOMContentLoadedEventArgs arg)
{
    _ = _iWebView2.ExecuteScriptAsync(
            "let content = document.createElement(\"h2\");" +
            "content.style.color = 'blue';" +
            "content.textContent = \"This text was added by the host app\";" +
            "document.body.appendChild(content);");
}

Applies to