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.