System.Windows.Controls.WebBrowser的焦点问题 [英] Focus issues with System.Windows.Controls.WebBrowser

查看:130
本文介绍了System.Windows.Controls.WebBrowser的焦点问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在与SP1一起发布的新WPF WebBrowser控件以及Windows Forms WebBrowser托管在WindowsFormHost和ElementHost控件分别.

当由于弹出窗口或其他有意义的对话框窗口而失去焦点时,似乎会出现主要问题.当焦点返回到承载WebBrowser控件的原始Window/Form时,键盘焦点似乎停留在某处,而没有通过编程的方式将其重新放置到WebBrowser控件上.但是,我注意到在WebBrowser控件中显示的文档中的某处单击鼠标似乎重新获得了焦点.

要使用下面发布的代码来重现此代码,请按照以下步骤操作:

I've been encountering several issues that deal with keyboard focus when it comes to the both the new WPF WebBrowser control that was released with SP1 as well as the Windows Forms WebBrowser in certain situations where they are hosted within the WindowsFormHost and the ElementHost controls respectively.

The main issue seems to come when focus is lost due to a popup window or some other meaningful dialog window. When focus returns to the original Window/Form which is hosting the WebBrowser control, the keyboard focus seems to be stuck somewhere without a programmatic a way to get it back onto the WebBrowser control. I have noticed however that clicking the mouse back somewhere within the document being displayed in the WebBrowser control seems to regain focus.

To reproduce this with the code that is posted below follow these steps:

  1. 将Form2作为主要应用程序表单运行
  2. 打开第一个表单后,尝试按Ctrl + N打开新的浏览器窗口
  3. 在新的浏览器窗口打开后关闭以便焦点自然返回到Form2
  4. 尝试使用Ctrl + N

再次打开新的浏览器窗口预期结果:新窗口浏览器窗口应与第2步中一样打开.

实际结果:新的浏览器窗口未打开,并且没有任何指示表明该应用程序正在识别该命令密钥. >

Expected Results: The new window browser window should open just like in step 2.

Actual Results: The new browser window does not open and no indication shows that the command key is being recognized by the application.


Form2.cs

使用 使用 System.Windows.Forms;
命名空间 WindowsFormsApplication1
{
> partial Form2:Form
{{
public Form2()
InitializeComponent();
System.Windows.Controls. WebBrowser w = System.Windows.Controls.WebBrowser();
w.Name = " webBrowser1"
系统. Windows.Controls.DockPanel d = System.Windows.Controls.DockPanel();
d.LastChildFill == true ;
d.Children.Add(w);
elementHost1.Child = d;
w.Navigate( Uri( " http://www.google .com" ));
}
using System; 
using System.Windows.Forms; 
 
namespace WindowsFormsApplication1 
    public partial class Form2 : Form 
    { 
        public Form2() 
        { 
            InitializeComponent(); 
 
            System.Windows.Controls.WebBrowser w = new System.Windows.Controls.WebBrowser(); 
            w.Name = "webBrowser1"
 
            System.Windows.Controls.DockPanel d = new System.Windows.Controls.DockPanel(); 
            d.LastChildFill = true
            d.Children.Add(w); 
 
            elementHost1.Child = d; 
 
            w.Navigate(new Uri("http://www.google.com")); 
        } 
    } 
 

推荐答案

好吧,我可以解释一下您遇到的情况:正常的WPF/hwnd互操作.将hwnds与WPF一起使用时,此行为非常标准.就定位和显示而言,这两种技术之间的互操作有效,但是不幸的是,这些焦点问题一直存在,并且没有人设法解决它们.我认为这就是为什么WPF/hwnd互操作不能在生产应用程序中使用的原因.
希望Microsoft的某人能够提出一种解决方法,可以在这种情况下为您提供帮助.不幸的是我不能.
Well, I can explain what you're experiencing: normal WPF/hwnd interop. This behaviour is pretty standard when using hwnds together with WPF. The interop between the two technologies works as far as positioning and display is concerned, but unfortunately these focus issues have always existed and nobody has managed to fix them. This is why, in my opinion, WPF/hwnd interop cannot be used in production applications.

Hopefully someone from Microsoft will be able to suggest a workaround that might help you in this individual case. Unfortunately I can't.


这篇关于System.Windows.Controls.WebBrowser的焦点问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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