question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WebView2.NavigateToString throws InvalidOperationException

See original GitHub issue

Describe the bug

WebView2.NavigateToString does not work in WinUI3 Preview3.

Steps to reproduce the bug

  1. Create a new Blank App (Windows in Desktop)

  2. Add the following below the Button on MainWindow.xaml <WebView2 x:Name="MyWebView" Width="600" Height="400" />

  3. Add this to myButton_Click in MainWindow.xaml.cs

MyWebView.NavigateToString("<html><body><h2>This is an HTML fragment</h2></body></html>");
  1. Run the app and click the button. See exception:
System.InvalidOperationException
  HResult = 0x80131509
  Message=A method was called at an unexpected time.

  Source=WinRT.Runtime
  StackTrace:
   at WinRT.ExceptionHelpers.ThrowExceptionForHR(Int32 hr)
   at ABI.Microsoft.UI.Xaml.Controls.IWebView2.global::Microsoft.UI.Xaml.Controls.IWebView2.NavigateToString(String htmlContent)
   at Microsoft.UI.Xaml.Controls.WebView2.NavigateToString(String htmlContent)
   at App75.MainWindow.myButton_Click(Object sender, RoutedEventArgs e) in C: \Users\matt\source\repos\App75\App75\App75\MainWindow.xaml.cs:line 35
   at ABI.Microsoft.UI.Xaml.RoutedEventHandler.<>c__DisplayClass10_0.<Do_Abi_Invoke>b__0(RoutedEventHandler invoke)
   at WinRT.ComWrappersSupport.MarshalDelegateInvoke[T](IntPtr thisPtr, Action`1 invoke)
   at ABI.Microsoft.UI.Xaml.RoutedEventHandler.Do_Abi_Invoke(IntPtr thisPtr, IntPtr sender, IntPtr e)

Expected behavior

The HTML content should be loaded in the WebView2 control.

Screenshots

Version Info

NuGet package version: Microsoft.WinUI 3.0.0-preview3.201113.0

Windows app type:

UWP Win32
Yes Yes
Windows 10 version Saw the problem?
Insider Build (xxxxx)
May 2020 Update (19041) Yes
November 2019 Update (18363)
May 2019 Update (18362)
October 2018 Update (17763)
April 2018 Update (17134)
Fall Creators Update (16299)
Creators Update (15063)
Device form factor Saw the problem?
Desktop Yes
Xbox
Surface Hub
IoT

Additional context

The same code works with a WebView (not 2) in a UWP (WinUI2) app.

As MediaElement will not be supported in the GA release of WinUI3, being able to use an embedded browser control was going to be a workaround. With this error, there is no workaround for needing to include embedded media content in an app. 😞

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:7
  • Comments:23 (4 by maintainers)

github_iconTop GitHub Comments

14reactions
dotMortencommented, Jan 30, 2021

For NavigateToString(), you should first call EnsureCoreWebView2Async()

This is a terrible developer experience. The SDK should be doing that for us.

2reactions
codendonecommented, Jun 1, 2021

@sigmarsson I recommend just a regular await:

    await myWebView2.EnsureCoreWebView2Async();
    myWebView2.NavigateToString("<html><body><h2>This is an HTML fragment</h2></body></html>");

(Or you could just do the EnsureCoreWebView2Async call once earlier, of course, if you don’t want to call this or check if CoreWebView2 is null before each place which will use it.)

Note: I assume the “new WebView2()” part of your example was simply for brevity, but for documentation purposes I’d like to note here that EnsureCoreWebView2Async() needs to be called on the WebView2 instance you want to ensure has a CoreWebView2. This is why my example above calls uses an instance for this call and the following use.

Read more comments on GitHub >

github_iconTop Results From Across the Web

InvalidOperationException in WebView2 ...
NavigateToString (HtmlDoc) (WebView2Control is the WebView2 instance). This call throws: InvalidOperationException: The instance of CoreWebView2 ...
Read more >
WebView2 control not loading HTML string
I never use 'NavigateToString', because having html in a C# string can quickly become 'messy'. Instead I create a subfolder (called 'html').
Read more >
WebView2.NavigateToString(String) Method
InvalidOperationException. Thrown when browser process has unexpectedly and left this control in an invalid state. We are considering throwing a different ...
Read more >
InvalidOperationException in WebView2.NavigateToString ...
I have a simple WinForm.Form hosting a WebView2 control. In the handler for the form's Load event, I hook the ...
Read more >
Microsoft.Web.WebView2.1.0.864.35\net45 ...
InvalidOperationException ">Thrown when this instance of <see cref="P:Microsoft.Web.WebView2.WinForms.WebView2.CoreWebView2"/> is already disposed.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found