question

StoutGamer avatar image
0 Votes"
StoutGamer asked HuiLiu-MSFT commented

WebView2 Is Not Displaying Web Pages.

Hi. I want to use the WebView2 to (a) display a website, and (b) display a local .pdf file on my WPF app.

https://docs.microsoft.com/en-us/microsoft-edge/webview2/get-started/wpf

The Microsoft documentation says I need to download two things on my PC as "prerequisites:"

  • Microsoft Edge WebView2

  • Microsoft Edge Insider Channels

For the first one I downloaded the "Evergreen Bootstrapper" (not the "Evergreen Standalone Installer" or "Fixed Version").
For the second one I downloaded the "Canary Channel" for Windows 10.

Are these the correct choices?

When I run the code without initialization, the WebView2 control is completely black and does not render the Microsoft website.

          xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"

         <wv2:WebView2 Name="webView" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Source="https://www.microsoft.com" />

Now when I try to call the InitializeAsync() method, it throws an exception:

     private async void InitializeWebView2Async()
     {
         try
         {
             await webView.EnsureCoreWebView2Async(null);
         }
         catch (Exception ex)
         {
         }
     }


Exception Message: "{"Unable to load DLL 'WebView2Loader.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)"}"

StackTrace " at Microsoft.Web.WebView2.Core.CoreWebView2Environment.CreateCoreWebView2EnvironmentWithOptions(String browserExecutableFolder, String userDataFolder, ICoreWebView2EnvironmentOptions options, ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler environment_created_handler)\r\n at Microsoft.Web.WebView2.Core.CoreWebView2Environment.<CreateAsync>d_3.MoveNext()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)\r\n at Microsoft.Web.WebView2.Wpf.WebView2.<>cDisplayClass26_0.<<EnsureCoreWebView2Async>gInit|0>d.MoveNext()\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.GetResult()\r\n at MyProject.MyUserControl.<InitializeWebView2Async>d_3.MoveNext() in C:\\Projects\\MyProject\\Views\\MyUserControl.xaml.cs:line 58" string


Thank you.


EDIT: Here is an update.

The solution contains two projects: A main WPF project and a separate WPF project. I want the code to be in the separate project that gets launched dynamically.

  • If I put the code in the main project, it works fine.

  • If I put the code in the separate project, it does not work at all.

  • If I put the code in both projects, it works fine in both projects.

So my workaround is adding the code in the main project with Visibility="Collapsed" and Width and Height set to "1."

Is there a better solution for that?

Thanks.




windows-wpfdotnet-wpf-xaml
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


It seems to work well after performing Steps 1 – 3, without additional installations (which maybe were required in the past). Do you build a .NET Framework or .NET Core or .NET 5 project?


1 Vote 1 ·

Thank you for the reply. It's a .NET Framework 4.7.2 app.

0 Votes 0 ·

Hi,@StoutGamer. I don't quite understand what does main WPF project and separate WPF project mean? What is the difference between your main project and a separate project?

0 Votes 0 ·

1 Answer

Castorix31 avatar image
1 Vote"
Castorix31 answered StoutGamer commented

On my OS (Windows 10 1909), VS 2019,
in a WPF .NET Framework App,
I just installed the WebView2 Runtime (MicrosoftEdgeWebView2RuntimeInstallerX64.exe),
then the Nuget Package... and that's all, it works fine

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for the suggestion. I've put in an "EDIT" if that can be useful.

0 Votes 0 ·