【问题标题】:How to have the WebBrowser properly running如何让 WebBrowser 正常运行
【发布时间】:2019-07-11 15:46:38
【问题描述】:

我使用 VS2017 在 C# 中管理一个 Excel 插件。

插件使用用于访问特定网页的 WebBrowser。 它最近停止在我拥有的每个版本的插件(开发和发布)上运行,即使我没有看到任何关于它的补丁说明。

基本上,WebBrowser 控件不再显示。它的所有属性都已正确设置,html 文档在导航时正确加载,但没有显示任何内容来代替控件。控件及其父组件设置为可见,并以正确的大小和坐标正确加载。

我已经使用 Visual Studio 设置了一个新的空白项目,并且只使用 WebBrowser 控件尝试显示 google 和 about:blank 对其进行了测试,但没有成功。 这是下面的代码:

ThisAddin.cs

    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
        {
            return new MyRibbon();
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion
    }

MyRibbon.cs

    [ComVisible(true)]
    public class MyRibbon : Office.IRibbonExtensibility
    {
        private Office.IRibbonUI ribbon;

        public MyRibbon()
        {
        }

        #region IRibbonExtensibility Members

        public string GetCustomUI(string ribbonID)
        {
            return GetResourceText("WebBrowserShowcase.MyRibbon.xml");
        }

        #endregion

        #region Ribbon Callbacks
        //Create callback methods here. For more information about adding callback methods, visit https://go.microsoft.com/fwlink/?LinkID=271226

        public void Ribbon_Load(Office.IRibbonUI ribbonUI)
        {
            this.ribbon = ribbonUI;
        }

        public void openWebBrowser_click(IRibbonControl control)
        {
            WebPanel panel = new WebPanel();
            panel.ShowWebBrowser();
        }

        public string getTabLabel(IRibbonControl control) { return "WebBrowserShowCase"; }

        #endregion
    }

MyRibbon.xml

<?xml version="1.0" encoding="UTF-8"?>
<customUI onLoad="Ribbon_Load" xmlns="http://schemas.microsoft.com/office/2009/07/customui">
  <ribbon>
    <tabs>
      <tab id="myRibbon" getLabel="getTabLabel">
        <group id="MyGroup" label="My Group">
          <button id="OpenWebBrowser" size="large" onAction="openWebBrowser_click" imageMso="_3DDirectionGalleryClassic"/>
        </group>
      </tab>
    </tabs>
  </ribbon>
</customUI>

WebPanel.cs

    public class WebPanel : UserControl
    {
        TableLayoutPanel tableLayoutPanel1;
        WebBrowser _webBrowser;

        public WebPanel()
        {
            InitializeComponent();
            WebBrowserHelper.FixBrowserVersion();
        }

        public void ShowWebBrowser()
        {
            CustomTaskPane taskPane = Globals.ThisAddIn.CustomTaskPanes.Add(this, "title", Globals.ThisAddIn.Application.ActiveWindow);
            DisplayTaskPane(taskPane);
            Navigate();
        }

        private void DisplayTaskPane(CustomTaskPane taskPane)
        {
            taskPane.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionLeft;
            taskPane.Width = Convert.ToInt32(Globals.ThisAddIn.Application.ActiveWindow.Width);
            taskPane.DockPositionRestrict = Microsoft.Office.Core.MsoCTPDockPositionRestrict.msoCTPDockPositionRestrictNoHorizontal;
            taskPane.Visible = true;

            tableLayoutPanel1.Dock = DockStyle.Fill;
            tableLayoutPanel1.Visible = true;

            _webBrowser.Dock = DockStyle.Fill;
            _webBrowser.Visible = true;
        }

        private void Navigate()
        {
            _webBrowser.Navigate(new Uri("about:blank"));
        }
    }

为方便起见,我删除了一些自动生成的代码和 WebBrowserHelper,您可以在这里找到:WebBrowserHelper

这是 WebPanel 的图像,它是 TableLayout 组件中的 WebBrowser:

这是启动项目时的结果,进入功能区并单击按钮:

如您所见,WebBrowser 未显示。我尝试了使用和不使用 Uri,about:blank 和 google,但结果始终相同。

有人知道这里可能存在什么问题吗?

编辑
显然,WebBrowser 仍然可以在 Windows 7 上运行,但不能在 Windows 10 上运行。 我已经用几台具有相同 Internet Explorer 版本(11.0.17134.829)的机器对其进行了检查。 如果是操作系统的问题,我能做些什么吗?

【问题讨论】:

    标签: c# controls webbrowser-control add-in


    【解决方案1】:

    我们遇到了同样的问题。有一个类似的线程here,,除了他在 Outlook 中遇到问题。确定的解决方法是在 Outlook 或 Excel >> 文件 >> 选项 >> 常规中从“优化以获得最佳外观”切换到“优化兼容性”。我可以确认这适用于我们的 Excel 插件。

    【讨论】:

    • 您好亚历克斯,感谢您的回答。我确认切换到兼容模式是有效的,不幸的是我不能依赖这种解决方法,因为我无法告诉和/或强制我的插件的用户这样做。不过,非常感谢您的提醒。
    猜你喜欢
    • 1970-01-01
    • 2011-11-12
    • 2017-03-03
    • 2010-09-27
    • 2013-12-24
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多