您是否尝试过此处描述的解决方法:https://support.microsoft.com/en-my/help/4490421/webbrowser-or-wpf-control-content-may-not-display-d-in-office
症状
在某些情况下,当控件托管在 CustomTaskPane 控件中时,WebBrowser 或 WPF 控件内容可能无法在 Office 应用程序中正确显示或运行。
原因
此问题可能是由 WebBrowser 或 WPF 控件本身引起的。不将焦点返回到在 WebBrowser 或 WPF 控件中呈现的 HTML 元素的宿主应用程序也可能导致此问题。
解决方法
要解决此问题,请在 CustomTaskPane 控件和 WebBrowser 或 WPF 控件之间添加一个中间表单。有关如何将自定义任务窗格添加到应用程序的详细信息,请参阅将自定义任务窗格添加到应用程序。
对于 WPF 控件呈现问题,我们建议您在 WindowsForm 中托管 WPF UserControl 并添加中间窗体。要添加中间窗体,请参阅演练:在 Windows 窗体中托管 3-D WPF 复合控件。
对于 WebBrowser 控件呈现问题,请使用 UserControl MyUserControl 来实现解决方法,如下所示:
// ThisAddIn.cs
namespace TaskPaneWorkaround
{
public partial class ThisAddIn
{
private MyUserControl myUserControl1;
private Microsoft.Office.Tools.CustomTaskPane myCustomTaskPane;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
myUserControl1 = new MyUserControl();
myCustomTaskPane = this.CustomTaskPanes.Add(myUserControl1, "My Task Pane");
myCustomTaskPane.Visible = true;
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
#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
}
}
// MyUserControl.cs
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace TaskPaneWorkaround
{
public partial class MyUserControl: UserControl
{
bool isformdisplayed = false;
WorkaroundForm workaroundForm;
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
public MyUserControl()
{
this.SuspendLayout();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Name = "MyUserControl";
this.Paint += new System.Windows.Forms.PaintEventHandler(this.MyUserControl_Paint);
this.Resize += new System.EventHandler(this.MyUserControl_Resize);
this.ResumeLayout(false);
this.Paint += MyUserControl_Paint;
this.Resize += MyUserControl_Resize;
}
private void MyUserControl_Load(object sender, System.EventArgs e)
{
this.Paint += MyUserControl_Paint;
}
private void MyUserControl_Paint(object sender, PaintEventArgs e)
{
if (!isformdisplayed)
{
this.SuspendLayout();
workaroundForm = new WorkaroundForm();
SetParent(workaroundForm.Handle, this.Handle);
workaroundForm.Dock = DockStyle.Fill;
workaroundForm.Width = Width;
workaroundForm.Height = Height;
workaroundForm.Show();
isformdisplayed = true;
this.ResumeLayout();
}
}
private void MyUserControl_Resize(object sender, EventArgs e)
{
if (isformdisplayed)
{
workaroundForm.Width = this.Width;
workaroundForm.Height = this.Height;
}
}
}
}
//WorkaroundForm.cs
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TaskPaneWorkaround
{
public partial class WorkaroundForm: Form
{
public WorkaroundForm()
{
this.SuspendLayout();
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(509, 602);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.WorkaroundForm_Load);
this.ResumeLayout(false);
}
private void WorkaroundForm_Load(object sender, EventArgs e)
{
this.SuspendLayout();
this.Location = new Point(0, 0);
this.Dock = DockStyle.Fill;
this.FormBorderStyle = FormBorderStyle.None;
WebBrowser webBrowser = new WebBrowser();
this.Controls.Add(webBrowser);
webBrowser.Location = new Point(0, 0);
webBrowser.Dock = DockStyle.Fill;
this.ResumeLayout();
webBrowser.Focus();
webBrowser.Navigate(new System.Uri("https://bing.com"));
}
}
}