【问题标题】:Webbrowsercontrol not loading URL in custom task pane - Word PluginWebbrowsercontrol 未在自定义任务窗格中加载 URL - Word 插件
【发布时间】:2018-09-06 06:38:57
【问题描述】:

我正在尝试在 WPF webbrowsercontrol 内的 word 插件自定义任务窗格 (VSTO) 中加载 Web 应用程序 URL。问题是它在开发机器和其他一些机器上运行良好,但它在某些机器上不起作用。以下是我的机器特定发现(所有机器都有 IE11):

我尝试过使用https://docs.microsoft.com/en-us/windows/communitytoolkit/controls/webview ,&&, https://blogs.windows.com/msedgedev/2018/05/09/modern-webview-winforms-wpf-apps/#34jyIaiQaMTrX2zR.97

根据一些线程的发现,我在网页上启用了 window.error 并删除了一些在 javascript 脚本中出现的错误。但在某些机器上,它会在 jquery 文件中给出未指定的错误。 我还尝试从注册表中为 WINWORD.EXE 设置浏览器仿真以使用 IE11。

附:我在目标框架 4.6 上开发了 vsto 插件。我也尝试过使用之前链接中提到的 WEBVIEW 控件所需的目标框架 4.6.2。此外,在 Windows 10 机器上启用 .net 4.7 版也不起作用。网页在 IE 中加载需要 3-4 秒。 网页使用jquery1.9和angular framework 1.6。并且该页面还包含一些网站上建议的 IE=edge 元标记。

是否有任何其他解决方法可以完成这项工作?

一些建议是关于在 WPF 中重新创建视图,由于严格的交付时间表,我无法在 WPF 中创建网页来显示和编辑所有数据。

【问题讨论】:

    标签: c# .net vsto webbrowser-control


    【解决方案1】:

    您是否尝试过此处描述的解决方法: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"));
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-25
      • 1970-01-01
      相关资源
      最近更新 更多