Closed minren118 closed 2 months ago
the error image below
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.
此异常最初是在此调用堆栈中引发的: [外部代码] 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
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;
}`