【问题标题】:WindowsFormsHost Winform pdfviewer control problemWindowsFormsHost Winform pdfviewer控件问题
【发布时间】:2020-01-07 16:11:59
【问题描述】:

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

【问题讨论】:

    标签: wpf windowsformshost


    【解决方案1】:

    不久前我遇到了这个问题。我记得这是一个与顶级 WPF 消息循环有关的错误,它与 WinForms 消息循环不兼容。

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

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

    new ElementHostForm(CreateContent(), 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 拥有最外层的消息循环来解决该错误。

    此更改对我来说非常容易,因为我已经在单独的 UserControl(而不是 Window)中拥有了我的顶级内容。如果您的顶级内容是 Window,您可能需要重构。

    【讨论】:

      猜你喜欢
      • 2014-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-15
      • 2012-02-04
      • 2019-07-19
      • 2012-04-01
      • 1970-01-01
      相关资源
      最近更新 更多