WindowsFormsHost Winform pdfviewer控件问题 [英] WindowsFormsHost Winform pdfviewer control problem

查看:121
本文介绍了WindowsFormsHost Winform pdfviewer控件问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a wpf Usercontrol, inside i am using a Winforms pdfviewer to display pdf files. Also i have couple of Textboxes to enter document details. finally, A popup which display this user control. The problem is, when i try to type something in textboxes, ntn is happenning. when i right click on a textbox, i can see context menu with cut, copy and paste options. After googling little bit, i found something like below, Forms.Integration.WindowsFormsHost.EnableWindowsFormsInterop(), I placed this line in loaded event but this is not working. can any one faced simillar issue and have any solutions. Thanks. Rey

推荐答案

我前一段时间遇到了这个问题.我记得这是一个与顶级WPF消息循环有关的错误,无法与WinForms消息循环配合使用.

I ran into this problem a while back. As I recall it was a bug having to do with the top level WPF message loop not playing nice with the WinForms message loop.

我使用的解决方案是将我的最外层从WPF窗口更改为WinForms表单.换句话说,我替换了

The solution I used was to change my outermost layer from a WPF Window to a WinForms Form. In other words, I replaced

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具有最外面的消息循环来解决该错误.

This worked around the bug by allowing WinForms to have the outermost message loop.

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

This change was very easy for me because I already had my top-level content in a separate UserControl (not a Window). If your top-level content is a Window you may need to refactor.

这篇关于WindowsFormsHost Winform pdfviewer控件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆