microsoft-ui-xaml: WebView2.NavigateToString throws InvalidOperationException

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. 😞

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 7
  • Comments: 26 (11 by maintainers)

Most upvoted comments

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

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

Closing this as the error message is updated on latest releases. image

I don’t think it’s much to ask to at least throw a helpful error message here. These are the kinds of razor cuts that are killing the developer experience

@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.

If this won’t be fixed, the exception that’s thrown should at least be informative and say that EnsureCoreWebView2Async needs to be called, rather than having devs result to a Google search to find this GitHub issue.