Amol Pawar
Amol Pawar

Reputation: 251

Webview2 is not working in WPF .Net Core App setup

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.

Upvotes: 3

Views: 2750

Answers (1)

Poul Bak
Poul Bak

Reputation: 10929

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.

Upvotes: 2

Related Questions