Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webview2 control for WPF does not work on target machine

Tags:

c#

wpf

webview2

I have a wpf WebView2 control that works on the development machine but does not work on the target machine, does not throw any errors either, the control is just blank. Its a wpf net 5 project. Microsoft.Web.WebView2 1.0.818.41

WebView2 control declared in xaml as

<wv2:WebView2 Name="WebBrowser" Grid.Row="1"  Source="https://www.microsoft.com"/>

Called in code:

public void LoadAddress(string webaddress)
{
    try
    {
        WebBrowser.Source=new Uri(webaddress);
    }
    catch(Exception e)
    {
        MessageBox.Show(e.Message);
    }
}

Initialized with

var env = await CoreWebView2Environment.CreateAsync(userDataFolder: WorkingDirectory);

The target machine has the WebView2 runtime installed.

When LoadAddress is called on the target machine nothing happens, no errors, the control is blank.

I use a function to check the version, that returns the right version on the target machine

version = typeof(CoreWebView2Environment).Assembly.GetName().Version;

I am using the Visual Studio installer to build the installer, this includes

"Microsoft.Web.WebView2": "1.0.818.41" in dependencies

  "Microsoft.Web.WebView2/1.0.818.41": {
    "runtime": {
      "lib/netcoreapp3.0/Microsoft.Web.WebView2.Core.dll": {
        "assemblyVersion": "1.0.818.41",
        "fileVersion": "1.0.818.41"
      },
      "lib/netcoreapp3.0/Microsoft.Web.WebView2.WinForms.dll": {
        "assemblyVersion": "1.0.818.41",
        "fileVersion": "1.0.818.41"
      },
      "lib/netcoreapp3.0/Microsoft.Web.WebView2.Wpf.dll": {
        "assemblyVersion": "1.0.818.41",
        "fileVersion": "1.0.818.41"
      }
    },
    "runtimeTargets": {
      "runtimes/win-arm64/native/WebView2Loader.dll": {
        "rid": "win-arm64",
        "assetType": "native",
        "fileVersion": "1.0.818.41"
      },
      "runtimes/win-x64/native/WebView2Loader.dll": {
        "rid": "win-x64",
        "assetType": "native",
        "fileVersion": "1.0.818.41"
      },
      "runtimes/win-x86/native/WebView2Loader.dll": {
        "rid": "win-x86",
        "assetType": "native",
        "fileVersion": "1.0.818.41"
      }
    }
  }

Any insights would be helpful. Thanks

like image 236
bluejay Avatar asked Feb 01 '26 20:02

bluejay


1 Answers

I made some changes and got it working. The app is being installed to the target machine program files folder which is read only in regular user mode.

WebView2 attempts to create a local folder there and fails. To overcome this you have to specify another location for the WebView2 local folder that the user will have read write access from the app.

First ensure that the Source is absent in the xaml declaration of the webview2 control:

<wv2:WebView2 Name="WebBrowser" Grid.Row="1"/>

because declaring the Source instantiates the control before you can work on it.

Then create a initialize method right after InitializeComponent of the window or control that is something like this

public async void InitializeWebView()
        {
            try
            {
                var env = await CoreWebView2Environment.CreateAsync(null,"C:\Temp");
                await TopicWebBrowser.EnsureCoreWebView2Async(env);
                TopicWebBrowser.Source = new Uri("https://www.google.com");
            }
            catch(Exception e)
            {
                MessageBox.Show(" Error in Web View "+e.Message, "Application");
            }
        }
like image 70
bluejay Avatar answered Feb 03 '26 09:02

bluejay