3

I have created a C# Outlook VSTO project with a wpf User control which is embedded in a Windows Form.

The idea is to navigate to an specific website using the WebView2 control that was added to the wpf User control.

The issue is that the control isn't rendering any website. On the other hand when I use a WebView2 control in a different project just with the Windows Form or WPF it works.

The package I'm using "Microsoft.Web.WebView2"

This is my Windows Form code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FraudDetector.Controls
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            this.eHost.Child = new FDView();
        }
    }
}

This is my wpf xaml:

<UserControl
             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:local="clr-namespace:FraudDetector.Controls"
             xmlns:Wpf="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid Background="Red">
        <Wpf:WebView2 Source="https://www.google.com/"/>
    </Grid>
</UserControl>

Do some one have some idea?

Jimi
  • 29,621
  • 8
  • 43
  • 61
  • Why a WPF UserControl? Can't you add the WebView2 Control directly to a Form? -- Where / when is the WebView2 instance initialized (i.e., when / how are you calling `EnsureCoreWebView2Async()`)? – Jimi May 11 '22 at 04:43
  • Yes, I can. This is another aproach I've used without success. Windows form with a WebView2 control embedded: public MainForm() { InitializeComponent(); InitWebView(); } async void InitWebView() { webView.Top = 0; webView.Left = 0; webView.Size = this.Size; await webView.EnsureCoreWebView2Async(null); webView.CoreWebView2.Navigate("https://www.google.com/"); } – Marcos Churi May 11 '22 at 16:09
  • 1
    Wrong method call. Subscribe to the `Load` event (or override `OnLoad()`) and make the handler `async`. There you `await EnsureCoreWebView2Async(...);` -- You should also specify what version of WebView2 you're targeting (make sure you don't have a *deprecated* - i.e., bugged as hell - version. The NuGet Package Manager tells you that). -- It's not clear what the .Net version of your app is. – Jimi May 11 '22 at 16:31
  • Don't use WPF with web browsers in Office applications. That is really a bad idea. You can place the browser on the Windows form, I guess it will be a user form in case of form regions. – Eugene Astafiev May 11 '22 at 21:04
  • - WebView2 version is 1.0.1210.39 (not deprecated) - .Net version is 4.7.2 My code after your recommendations: public MainForm() { InitializeComponent(); this.Load += new EventHandler(Form_Load); } private async void Form_Load(object sender, EventArgs e) { webView.Top = 0; webView.Left = 0; webView.Size = this.Size; await webView.EnsureCoreWebView2Async(null); webView.CoreWebView2.Navigate("https://www.google.com/"); } – Marcos Churi May 11 '22 at 21:29
  • @EugeneAstafiev why is that a bad idea? – gusmally supports Monica Jan 18 '23 at 18:59

1 Answers1

1

Here's the solution I found to set the userDataFolder (also called "cache") in WebView2:

The tempWebCacheDir is the directory where the userDataFolder will be set. CoreWebView2Environment.CreateAsync(userDataFolder: tempWebCacheDir) is used to create a new CoreWebView2Environment with the userDataFolder set to the specified directory.

using Microsoft.Web.WebView2.Core;

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
    }

    private async void MainForm_Load(object sender, EventArgs e)
    {
        await InitializeCoreWebView2Async();
        //Navigate to URI by setting Source property
        webView.Source = new Uri("https://www.google.com/");
    }

    private async Task InitializeCoreWebView2Async()
    {
        string tempWebCacheDir = @"C:\Temp";
        //Specify options regarding the coreView2 initialization process
        var webView2Environment = await CoreWebView2Environment.CreateAsync(userDataFolder: tempWebCacheDir);
        //CoreWebView2 creation
        await webView.EnsureCoreWebView2Async(webView2Environment);
    }
}
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 20 '22 at 00:31