Vicente Flich
Vicente Flich

Reputation: 29

WebView2 not showing content from string

I'm trying WebView2 control in WinUI 3 with C++/WinRT to show a content from an string like this:

std::string htmlContent = R"(
    <html>
    <body>
        <h1>Hello world!</h1>
    </body>
    </html>
)";
MyWebView.NavigateToString(winrt::to_hstring(htmlContent));

Nothing is showed in my WebView control. But if I try it from a web URI, it work fine:

Uri url(L"https://www.google.com");
MyWebView.Source(url);

I remember having test it a couple months ago, and it worked with NavigateToString(), but now it didn't.

Maybe is a problem with the last version of the App SDK? (I'm working with version 1.0.6065.39 version for Microsoft.Web.WebView2 and 1.6.250205002 for Microsoft.WindowsAppSDK).

Any idea?

Upvotes: -4

Views: 48

Answers (1)

Vicente Flich
Vicente Flich

Reputation: 29

Achieved. My mistake were I need to wait the WebView2 to complete. The correct code is:

std::string htmlContent = R"(
    <html>
    <body>
        <h1>Hello world!</h1>
    </body>
    </html>
    )";
MyWebView.EnsureCoreWebView2Async().Completed([MyWebView,htmlContent](auto&&, auto&&) {
    MyWebView.NavigateToString(winrt::to_hstring(htmlContent));
});

Upvotes: -1

Related Questions