weixin_45840746 2023-12-27 10:13 采纳率: 35.6%
浏览 11
已结题

vsto:powerpoint的外接程序中使用webview2

在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
    }

}

  • 写回答

6条回答 默认 最新

  • 技术宅program 2023-12-27 10:29
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

问题事件

  • 系统已结题 1月4日
  • 已采纳回答 12月27日
  • 创建了问题 12月27日

悬赏问题

  • ¥30 模拟电路 logisim
  • ¥15 PVE8.2.7无法成功使用a5000的vGPU,什么原因
  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价