3

We have installed Microsoft Edge WebView2 runtime(x86) for WebView2. Refer below SS.

enter image description here

We are using .NET Core 3.1 framework and WPF for our App development. Our webview2 runs fine in debug/release/publish mode in Visual Studio but it is not getting loaded once we install our application.

It is not giving any error also when we run app with normal privileges and also not getting loaded. When we run it as "Run as Administrator" it gives below error,

enter image description here

We are using Windows Installer Project extension in Visual studio for .MSI creation

Our XAML code is as below,

<UserControl x:Class="SampleApp.Views.SampleWebView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
             xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
             mc:Ignorable="d"
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="10"/>
            <ColumnDefinition Width="0.7*"/>
            <ColumnDefinition Width="0.3*"/>
            <ColumnDefinition Width="10"/>
        </Grid.ColumnDefinitions>
        <Border Grid.Column="1"  Margin="5" CornerRadius="10"  BorderThickness="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
            <Border.BitmapEffect>
                <DropShadowBitmapEffect Color="Black" Direction="40" ShadowDepth="2" Softness="1" Opacity="0.3" />
            </Border.BitmapEffect>
            <wv2:WebView2 x:Name="MyWebView" Source="{Binding UrlToGo}" Margin="5 2"/>
        </Border>
</Grid>

The UrlToGo is coming from our ViewModel.

Kindly help us to resolve.

Amol Pawar
  • 251
  • 1
  • 15

1 Answers1

2

The error says it all, it doesn't have write access to 'C:\Program files' where it wants to create the UserDataFolder(which contains cache and cookies etc).

The solution is to specify the UserDataFolder when creating the WebView2. You do that like this:

using Microsoft.Web.WebView2.Core;

string userDataFolder = Path.Combine(Path.GetTempPath(), "WhatEver");
webView21.CreationProperties = new CoreWebView2CreationProperties()
{
    UserDataFolder = userDataFolder
};
await webView21.EnsureCoreWebView2Async();

It's important that you DON'T set the Source property, before you have called EnsureCoreWebView2Async Of course replace 'webView2' with the name of your WebView2 control.

Poul Bak
  • 10,450
  • 5
  • 32
  • 57
  • 1
    Yes Poul Bak's answer is correct - you'll need to explicitly specify the user data folder. The problem is that the current default for the user data folder is the same folder as the host app executable. Usually when developing code the host app has permission to write to the debug folder. And usually when deployed by an installer into Program Files or similar the host app will not have permission. There is an upcoming change to the default user data folder that may help with this https://github.com/MicrosoftEdge/WebView2Announcements/issues/26 – David Risney Jun 30 '21 at 23:14
  • @poul Bak Thanks you for you response. It worked. We are using MVVM approach using PRISM framework. Can you tell me the event name in which we can keep this code so that it could be executed before our binding. This may sound silly but as i am new to WPF and MVVM, hence need your help. – Amol Pawar Jul 02 '21 at 07:58
  • Is this what you need? `protected override async void OnContentRendered(EventArgs e)` – Poul Bak Jul 02 '21 at 13:35
  • We are calling webview2 in usercontrol which is getting loaded when user navigate to that functionality . We do not have this override method in usercontrol. is there something that we need to inherit in the code behind class? or is there any other method for user control equivalent to this?" – Amol Pawar Jul 02 '21 at 14:39
  • Just define a public method in your usercontrol and call it after you have created it. – Poul Bak Jul 02 '21 at 14:55