WebView2 Is Not Displaying Web Pages.

Stout 286 Reputation points
2021-07-12T07:18:42.13+00:00

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

https://learn.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.<>c__DisplayClass26_0.<<EnsureCoreWebView2Async>g__Init|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.

Developer technologies | Windows Presentation Foundation
Developer technologies | XAML
Developer technologies | XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
{count} votes

1 answer

Sort by: Most helpful
  1. Castorix31 91,511 Reputation points
    2021-07-12T08:14:58.757+00:00

    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 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.