WebView2.NavigateToString throws InvalidOperationException
See original GitHub issueDescribe the bug
WebView2.NavigateToString
does not work in WinUI3 Preview3.
Steps to reproduce the bug
-
Create a new Blank App (Windows in Desktop)
-
Add the following below the
Button
onMainWindow.xaml
<WebView2 x:Name="MyWebView" Width="600" Height="400" />
-
Add this to
myButton_Click
inMainWindow.xaml.cs
MyWebView.NavigateToString("<html><body><h2>This is an HTML fragment</h2></body></html>");
- 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:
- Created 3 years ago
- Reactions:7
- Comments:23 (4 by maintainers)
Top GitHub Comments
This is a terrible developer experience. The SDK should be doing that for us.
@sigmarsson I recommend just a regular await:
(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.