C# 如何让WebBrowser正常运行
我使用VS2017在C#中管理Excel加载项 加载项使用用于访问特定网页的WebBrowser。 它最近停止在我所有的插件版本(开发版和发布版)上运行,尽管我还并没有看到任何关于它的补丁说明 基本上,WebBrowser控件不再显示。它的所有属性都已正确设置,html文档在导航时已正确加载,但没有显示任何内容来代替控件。控件及其父组件被设置为可见,并使用正确的大小和坐标正确加载 我用visualstudio设置了一个新的blank项目,并用一个WebBrowser控件测试了它,该控件试图显示google和about:blank,但没有成功。 代码如下: 本附件cs:C# 如何让WebBrowser正常运行,c#,controls,webbrowser-control,add-in,C#,Controls,Webbrowser Control,Add In,我使用VS2017在C#中管理Excel加载项 加载项使用用于访问特定网页的WebBrowser。 它最近停止在我所有的插件版本(开发版和发布版)上运行,尽管我还并没有看到任何关于它的补丁说明 基本上,WebBrowser控件不再显示。它的所有属性都已正确设置,html文档在导航时已正确加载,但没有显示任何内容来代替控件。控件及其父组件被设置为可见,并使用正确的大小和坐标正确加载 我用visualstudio设置了一个新的blank项目,并用一个WebBrowser控件测试了它,该控件试图显示g
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
}
[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
}
<?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>
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"));
}
}
MyRibbon.xml:
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
}
[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
}
<?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>
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:
这是WebPanel的图像,它是TableLayout组件中的WebBrowser:
以下是启动项目、进入功能区并单击按钮时的结果:
如您所见,WebBrowser不显示。我尝试了使用和不使用Uri,关于:blank和google,但结果总是一样的
有人知道这里可能有什么问题吗
编辑:显然,WebBrowser在Windows7上仍然有效,但在Windows10上不起作用。 我用几台都有相同internet explorer版本(11.0.17134.829)的机器进行了检查。
如果操作系统出了问题,我能做些什么吗?我们也遇到了同样的问题。除了他在Outlook中遇到问题外,还有一条类似的线索。确定的解决方法是在Outlook或Excel>>文件>>选项>>常规中从“优化最佳外观”切换到“优化兼容性”。我可以确认这对我们的Excel加载项有效 你好,亚历克斯,谢谢你的回答。我确认切换到兼容模式是可行的,不幸的是,我不能依赖此解决方案,因为我不能告诉和/或强制我的加载项的用户这样做。尽管如此,我们还是非常感谢你的提醒。