how to use webview2 in CustomTaskPane with ExcelDna project?
minren118 opened this issue · 4 comments
Using the VSTO project, I was able to implement the webview2 display on the CustomTaskPane.
but in Exceldna project,it will make Error in await webView.EnsureCoreWebView2Async(webViewEnvironment);
the whole MyCustomTaskPane code is below。also in form window is normal with exceldna project ,only CustomTaskPane fail
I use ExcelDna.Integration 1.1.0.0 and .net framework4.62 in exceldna project.
`using System;
using System.Windows.Forms;
using Microsoft.Web.WebView2.WinForms;
using Microsoft.Web.WebView2.Core;
using System.IO;
using System.Threading.Tasks;
public partial class MyCustomTaskPane : UserControl
{
private WebView2 webView;
public MyCustomTaskPane()
{
InitializeWebView2();
}
private async void InitializeWebView2()
{
webView = new WebView2
{
Dock = DockStyle.Fill
};
Controls.Add(webView);
string userDataFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MyAppWebView2Data");
CoreWebView2Environment webViewEnvironment = await CoreWebView2Environment.CreateAsync(null, userDataFolder);
await webView.EnsureCoreWebView2Async(webViewEnvironment);
webView.CoreWebView2.Navigate("https://www.baidu.com");
}
}`
System.Runtime.InteropServices.COMException
HResult=0x80004004
Message=已中止操作 (异常来自 HRESULT:0x80004004 (E_ABORT))
Source=mscorlib
StackTrace:
在 System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
在 Microsoft.Web.WebView2.Core.CoreWebView2Environment.d__98.MoveNext()
在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
在 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
在 Microsoft.Web.WebView2.WinForms.WebView2.d__25.MoveNext()
在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
在 System.Runtime.CompilerServices.TaskAwaiter.GetResult()
在 MyCustomTaskPane.d__2.MoveNext() 在 D:\VisualStudio项目\EasyShu\WebView2Test\MyCustomTaskPane.cs 中: 第 28 行
此异常最初是在此调用堆栈中引发的:
[外部代码]
MyCustomTaskPane.InitializeWebView2() (位于 MyCustomTaskPane.cs 中)
This is the code we use to instantiate the webView2
private static async Task<CoreWebView2Environment> WebView2EnvironmentAsync()
{
if (webView2Environment == null)
{
Environment.SetEnvironmentVariable("COREWEBVIEW2_FORCED_HOSTING_MODE", "COREWEBVIEW2_HOSTING_MODE_WINDOW_TO_VISUAL"); // https://github.com/MicrosoftEdge/WebView2Feedback/issues/951#issuecomment-1064624832
string localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string cacheFolder = Path.Combine(localAppData, "WindowsFormsWebView2");
LogDisplay.RecordLine($"Available Browser Version String is {CoreWebView2Environment.GetAvailableBrowserVersionString()}, cache folder is {cacheFolder}");
webView2Environment = await CoreWebView2Environment.CreateAsync(null, cacheFolder);
LogDisplay.RecordLine($"CoreWebView2Environment is {webView2Environment}");
}
return webView2Environment;
}
WebView2 webView21;
var environment = await WebView2EnvironmentAsync();
await webView21.EnsureCoreWebView2Async(environment);
This is the code we use to instantiate the webView2
private static async Task<CoreWebView2Environment> WebView2EnvironmentAsync() { if (webView2Environment == null) { Environment.SetEnvironmentVariable("COREWEBVIEW2_FORCED_HOSTING_MODE", "COREWEBVIEW2_HOSTING_MODE_WINDOW_TO_VISUAL"); // https://github.com/MicrosoftEdge/WebView2Feedback/issues/951#issuecomment-1064624832 string localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); string cacheFolder = Path.Combine(localAppData, "WindowsFormsWebView2"); LogDisplay.RecordLine($"Available Browser Version String is {CoreWebView2Environment.GetAvailableBrowserVersionString()}, cache folder is {cacheFolder}"); webView2Environment = await CoreWebView2Environment.CreateAsync(null, cacheFolder); LogDisplay.RecordLine($"CoreWebView2Environment is {webView2Environment}"); } return webView2Environment; }
WebView2 webView21; var environment = await WebView2EnvironmentAsync(); await webView21.EnsureCoreWebView2Async(environment);
I user the above code,but also fail ,my windows is win11,so Environment.SetEnvironmentVariable("COREWEBVIEW2_FORCED_HOSTING_MODE", "COREWEBVIEW2_HOSTING_MODE_WINDOW_TO_VISUAL"); is not necessary ,thanks anything
I found out why. I didn't give MyCustomTaskPane a [ComVisible(true)] attribute. I made a stupid mistake, sorry