WPF + XNA:WPF TextBox 不处理按键输入
我正在尝试使用 WPF 窗口中的 WindowsFormsHost 控件将 XNA 集成到 WPF 窗口中。我遇到一个非常奇怪的问题,每当 XNA 运行时,我的 WPF 窗口控件中的键盘输入就不起作用。更奇怪的是,并非所有按键都会发生这种情况。所有字母和数字都会发生这种情况,但例如退格或删除关键工作。我首先认为这是由于 XNA 连接到键盘输入并处理所有按键,并且事件没有被路由到我的 WPF 窗口。但奇怪的是,它们确实由 OnKeyDown 处理。我创建了一个自定义 TextBox 类,该类派生自普通 TextBox,重写了 OnKeyDown,放入断点,并且它起作用了。调试器踩到了断点。所以我错了:XNA 没有阻止按键事件到达 WPF 窗口(或其控件),我的 TextBox 正在获取正确的键盘事件,但它不起作用。我按键盘上的“A”,OnKeyDown 被调用,但它不会将“A”附加到文本框中。另一方面,退格键可以正常工作。我使用鼠标右键单击将一些文本复制粘贴到字段中,然后单击文本的中间(箭头也不起作用),我可以通过按退格键删除文本,但我无法输入任何内容。
可能是什么原因?我不太擅长 WPF 的输入处理,但通常情况下,如果我的 TextBox 确实获得了 OnKeyDown 事件(带有正确的参数),它应该附加我按下的字母。一旦我结束 XNA 游戏,一切就开始正常运行。控制行为异常的原因可能是什么?
I'm trying to integrate XNA into a WPF window, using the WindowsFormsHost control in the WPF window. I've got a very strange problem that whenever XNA is running, my keyboard input into my WPF window controls don't work. More weirdly, this is happening not for all keys. It happens for all the letters and numbers, but, for example, backspace or delete key work. I first thought this was due to XNA hooking up to the keyboard input and handling all the key presses, and the events weren't being routed to my WPF window. But weirdly, they DO get handled by the OnKeyDown. I've created a custom TextBox class that derives from normal TextBox, overrided the OnKeyDown, put a breakpoint in, and it worked. The debugger stepped on the breakpoint. So I was wrong: XNA wasn't blocking the key events from reaching the WPF window (or its controls), my TextBox IS getting the correct keyboard event, but it just doesn't work. I press 'A' on my keyboard, OnKeyDown gets called, but it doesn't append 'A' into the textbox. The backspace key, on the other hand, works normally. I copy paste some text into the field using mouse right click, and I click on the middle of the text (arrows don't work too), I can delete the text by pressing backspace, but I can't type anything.
What can be the reason? I am not really good with WPF's input handling, but normally, if my TextBox DOES get the OnKeyDown event (with the right argument), it should append the letter that I've pressed. As soon as I end the XNA game, things start behaving correctly. What can be the reason for the controls behaving abnormally?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
其他人在这里遇到了同样的问题: http://forums.create .msdn.com/forums/p/98145/584961.aspx 他们已经找到了问题的解决方案(他们也链接到了这个问题)
XNA 和 WPF 的互操作性需要进行一些调整:
System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop(this);
此代码应粘贴到将托管XNA 内容。我已将其粘贴到
InitializeComponent()
之后的构造函数中,并且它有效。Someone else has ran into the same problem here: http://forums.create.msdn.com/forums/p/98145/584961.aspx and they've found the solution to the problem (they've also linked to this question, too)
The interoperability of XNA and WPF needs a little tweak:
System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop(this);
This code should be pasted to the constructor of the WPF window that will be hosting the XNA content. I've pasted it to the constructr after
InitializeComponent()
and it worked.