WebView2在WPF/Windows表单内部的WebView2 Control for VSTO Project

发布于 2025-01-27 15:46:58 字数 1506 浏览 3 评论 0原文

我已经创建了一个使用WPF用户控件的 C#Outlook VSTO项目,该项目嵌入了Windows表单

这个想法是使用添加到WPF用户控件中的WebView2控件导航到特定网站。

问题是该控件没有渲染任何网站。另一方面,当我在其他项目中使用windows表单或wpf在其他项目中使用WebView2控件时。

我正在使用“ Microsoft.web.webview2”

这是我的Windows表单代码:

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();
        }
    }
}

这是我的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>

有人有一些想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光礼记 2025-02-03 15:46:58

这是我发现在WebView2中设置UserDataFolder(也称为“ CACHE”)的解决方案:

tempwebcachedir 是设置用户datafolder的目录。
corewebview2environment.createasync(userDataFolder:tempwebcachedir)用于创建一个新的corewebview2enview2environment,并将用户datafolder设置为指定目录。

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);
    }
}

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文