在powerpoint的外接程序中使用webview2,我希望实现在任务窗口上打开网页,但是我初始化完成后任务窗口总是空白,请教一下,我该怎么解决哎,不要用gpt之类的AI回答了,试过了。谢谢
using System;
using Microsoft.Web.WebView2.WinForms;
using System.Windows.Forms;
using System.Threading.Tasks;
using Microsoft.Web.WebView2.Core;
using System.IO;
using System.Diagnostics;
namespace PowerPointAddIn2
{
public partial class ThisAddIn
{
private WebView2 webView;
private async void ThisAddIn_Startup(object sender, EventArgs e)
{
// 初始化 WebView2 控件
await InitializeWebView();
}
private async Task InitializeWebView()
{
try
{
var userDataFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "MyAppDataFolder");
Debug.WriteLine($"userDataFolder:{userDataFolder}");
var environment = await CoreWebView2Environment.CreateAsync(null, userDataFolder);
webView = new WebView2();
webView.CoreWebView2InitializationCompleted += WebViewInitializationCompleted;
var taskPane = this.CustomTaskPanes.Add(new UserControl { Controls = { webView } }, "WebView Task Pane");
taskPane.Visible = true;
await webView.EnsureCoreWebView2Async(environment);
webView.NavigationCompleted += (sender, args) =>
{
if (args.IsSuccess) Debug.WriteLine("Navigation succeeded");
else Debug.WriteLine($"Navigation failed with status: {args.WebErrorStatus}");
};
Debug.WriteLine("Initialization completed on UI thread.");
}
catch (Exception ex)
{
MessageBox.Show($"Error: {ex.Message}");
}
}
private void WebViewInitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
{
if (e.IsSuccess)
{
Debug.WriteLine("WebView2 initialization succeeded");
webView.Source = new Uri("https://www.baidu.com");
}
else Debug.WriteLine($"WebView2 initialization failed: {e.InitializationException}");
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
#region VSTO 生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}