Wpf WindowsFormsHost Winform pdfviewer控制问题

Wpf WindowsFormsHost Winform pdfviewer控制问题,wpf,windowsformshost,Wpf,Windowsformshost,我有一个wpf用户控件,里面我使用Winforms pdfviewer显示pdf文件。我还有几个文本框来输入文档详细信息。最后,显示此用户控件的弹出窗口。 问题是,当我尝试在文本框中键入内容时,ntn正在发生。当我在文本框上单击鼠标右键时,我可以看到带有剪切、复制和粘贴选项的上下文菜单。通过谷歌搜索,我发现如下内容,Forms.Integration.WindowsFormsHost.EnableWindowsFormsInterop(),我将这一行放在加载的事件中,但这不起作用。任何人都可以面

我有一个wpf用户控件,里面我使用Winforms pdfviewer显示pdf文件。我还有几个文本框来输入文档详细信息。最后,显示此用户控件的弹出窗口。 问题是,当我尝试在文本框中键入内容时,ntn正在发生。当我在文本框上单击鼠标右键时,我可以看到带有剪切、复制和粘贴选项的上下文菜单。通过谷歌搜索,我发现如下内容,
Forms.Integration.WindowsFormsHost.EnableWindowsFormsInterop()
,我将这一行放在加载的事件中,但这不起作用。任何人都可以面对类似的问题,并有任何解决方案。 谢谢
Rey

不久前我遇到了这个问题。我记得,这是一个与顶级WPF消息循环有关的bug,不能很好地处理WinForms消息循环

我使用的解决方案是将最外层从WPF窗口更改为WinForms窗体。换言之,我替换了

new Window { Content = CreateContent(), Title = title }.Show();

通过使用这样的类:

class ElementHostForm : System.Windows.Forms.Form
{
  ElementHost _host;

  public WinFormsWindow(UIElement content, string title)
  {
    _host = new ElementHost { Child = content };
    Controls.Add(host);

    content.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
    if(content.DesiredSize.Width > 100 && content.DesiredSize.Height > 100)
      ClientSize = _host.Size =
        new Size((int)content.DesiredSize.Width, (int)content.DesiredSize.Height));

    content.ClearValue(FrameworkElement.WidthProperty);
    content.ClearValue(FrameworkElement.HeightProperty);

    Title = title;
  }

  protected override void OnResize(EventArgs e)
  {
    if(!ClientSize.IsEmpty) _host.Size = ClientSize;
    base.OnResize(e);
  }
}
这通过允许WinForms具有最外层的消息循环来解决该错误

这个更改对我来说非常简单,因为我已经在一个单独的用户控件(而不是窗口)中拥有了顶级内容。如果顶级内容是一个窗口,则可能需要重构

class ElementHostForm : System.Windows.Forms.Form
{
  ElementHost _host;

  public WinFormsWindow(UIElement content, string title)
  {
    _host = new ElementHost { Child = content };
    Controls.Add(host);

    content.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
    if(content.DesiredSize.Width > 100 && content.DesiredSize.Height > 100)
      ClientSize = _host.Size =
        new Size((int)content.DesiredSize.Width, (int)content.DesiredSize.Height));

    content.ClearValue(FrameworkElement.WidthProperty);
    content.ClearValue(FrameworkElement.HeightProperty);

    Title = title;
  }

  protected override void OnResize(EventArgs e)
  {
    if(!ClientSize.IsEmpty) _host.Size = ClientSize;
    base.OnResize(e);
  }
}