Specifically, I have a button that opens a non-modal child window. Something in the main window steals the focus away from the child window as it opens.
具体地说,我有一个打开非模态子窗口的按钮。当子窗口打开时,主窗口中的某些东西将焦点从子窗口中偷走。
The fully general approach is to comment out parts of code until the problem goes away. I'm looking for faster methods.
完全通用的方法是注释代码的一部分,直到问题消失。我在寻找更快的方法。
6 个解决方案
#1
21
I'm using Snoop to do this right now.
我现在正用史努比做这个。
For starters, Snoop shows the current focused element and the current FocusScope in the status bar.
首先,Snoop在状态栏中显示当前焦点元素和当前焦点范围。
You can get it to show you all the GotFocus and LostFocus events:
你可以让它向你展示所有的GotFocus和LostFocus事件:
- Run your app.
- 运行您的应用程序。
- Run Snoop.
- 爱管闲事的人。
- Choose your app in the dropdown.
- 在下拉菜单中选择你的应用。
- Click the binoculars ("Snoop") button.
- 点击双筒望远镜(“Snoop”)按钮。
- On the right pane, click the Events tab.
- 在右边窗格中,单击Events选项卡。
- Click to bring down the dropdown.
- 单击以使下拉下降。
- Scroll down to the Keyboard section and check GotKeyboardFocus, LostKeyboardFocus, and optionally the PreviewXXX events.
- 向下滚动到键盘部分,检查GotKeyboardFocus、LostKeyboardFocus和PreviewXXX事件。
- Now do what you need to do to manipulate focus and watch the Snoop window.
- 现在做你需要做的来操作焦点和观察窥探窗口。
Similarly you can track the FocusManager events the same way.
类似地,您可以以相同的方式跟踪FocusManager事件。
#2
0
I haven't tried this myself so can't tell exact steps, but you can try to use FocusManager.FocusedElement and FocusManager.GetFocusedElement Method to try and find the element stealing focus from your child window.
我自己还没试过,所以说不出具体的步骤,但是你可以尝试使用FocusManager。FocusedElement FocusManager。GetFocusedElement方法试图找到从子窗口窃取焦点的元素。
Other useful link -
其他有用的链接,
Focus Overview(Have code for getting focused element): http://msdn.microsoft.com/en-us/library/aa969768.aspx#Focus_Events
焦点概述(有获得焦点元素的代码):http://msdn.microsoft.com/en-us/library/aa969768.aspx#Focus_Events。
#3
#4
0
Could you please mention the main operations (without going into client specific business - if it is a client project) - like is there any events hooked on to the elements, Any other operation happening on the button click - Anything which happens for any element in the parent window after the child window is shown which steals the focus. Also check any related events hooked which happen during button click event.
请您提到的主要操作(没有进入客户特定的业务——如果这是一个客户端项目)——就像有什么事件连接的元素,任何其他操作发生在按钮点击——任何发生的任何元素的父窗口后的子窗口显示了焦点。还要检查在按钮单击事件期间连接的任何相关事件。
These are some of the ways you can start your investigation.
这些是你开始调查的一些方法。
#5
0
I just read about some deadlock...hope this helps (search for control.leave in msdn.microwoft.com
我刚看到一些死锁……希望这能有所帮助。离开在msdn.microwoft.com
Caution Do not attempt to set focus from within the Enter, GotFocus, Leave, LostFocus, Validating, or Validated event handlers. Doing so can cause your application or the operating system to stop responding. For more information, see the WM_KILLFOCUS topic in the "Keyboard Input Reference" section, and the "Message Deadlocks" section of the "About Messages and Message Queues" topic in the MSDN library at http://msdn.microsoft.com/library.
注意,不要试图在Enter、GotFocus、Leave、LostFocus、验证或验证事件处理程序中设置焦点。这样做会导致应用程序或操作系统停止响应。有关更多信息,请参见MSDN库http://msdn.microsoft.com/library中的“键盘输入引用”部分中的WM_KILLFOCUS主题,以及“关于消息和消息队列”主题的“消息死锁”部分。
#6
0
You can try to track calls to the System.Window.UIElement.Focus method (in the PresentationCore.dll) in the context of your application using Runtime Flow (developed by me).
您可以尝试跟踪对system . windows . uielement的调用。使用运行时流(由我开发)在应用程序的上下文中集中方法(在PresentationCore.dll中)。
#1
21
I'm using Snoop to do this right now.
我现在正用史努比做这个。
For starters, Snoop shows the current focused element and the current FocusScope in the status bar.
首先,Snoop在状态栏中显示当前焦点元素和当前焦点范围。
You can get it to show you all the GotFocus and LostFocus events:
你可以让它向你展示所有的GotFocus和LostFocus事件:
- Run your app.
- 运行您的应用程序。
- Run Snoop.
- 爱管闲事的人。
- Choose your app in the dropdown.
- 在下拉菜单中选择你的应用。
- Click the binoculars ("Snoop") button.
- 点击双筒望远镜(“Snoop”)按钮。
- On the right pane, click the Events tab.
- 在右边窗格中,单击Events选项卡。
- Click to bring down the dropdown.
- 单击以使下拉下降。
- Scroll down to the Keyboard section and check GotKeyboardFocus, LostKeyboardFocus, and optionally the PreviewXXX events.
- 向下滚动到键盘部分,检查GotKeyboardFocus、LostKeyboardFocus和PreviewXXX事件。
- Now do what you need to do to manipulate focus and watch the Snoop window.
- 现在做你需要做的来操作焦点和观察窥探窗口。
Similarly you can track the FocusManager events the same way.
类似地,您可以以相同的方式跟踪FocusManager事件。
#2
0
I haven't tried this myself so can't tell exact steps, but you can try to use FocusManager.FocusedElement and FocusManager.GetFocusedElement Method to try and find the element stealing focus from your child window.
我自己还没试过,所以说不出具体的步骤,但是你可以尝试使用FocusManager。FocusedElement FocusManager。GetFocusedElement方法试图找到从子窗口窃取焦点的元素。
Other useful link -
其他有用的链接,
Focus Overview(Have code for getting focused element): http://msdn.microsoft.com/en-us/library/aa969768.aspx#Focus_Events
焦点概述(有获得焦点元素的代码):http://msdn.microsoft.com/en-us/library/aa969768.aspx#Focus_Events。
#3
0
You can try using a tool like UI Spy or Snoop to watch for the event that changes the focus. I haven't used these tools for this purpose, but I believe they can help you.
您可以尝试使用UI Spy或Snoop这样的工具来监视更改焦点的事件。我还没有用过这些工具,但是我相信它们可以帮助你。
#4
0
Could you please mention the main operations (without going into client specific business - if it is a client project) - like is there any events hooked on to the elements, Any other operation happening on the button click - Anything which happens for any element in the parent window after the child window is shown which steals the focus. Also check any related events hooked which happen during button click event.
请您提到的主要操作(没有进入客户特定的业务——如果这是一个客户端项目)——就像有什么事件连接的元素,任何其他操作发生在按钮点击——任何发生的任何元素的父窗口后的子窗口显示了焦点。还要检查在按钮单击事件期间连接的任何相关事件。
These are some of the ways you can start your investigation.
这些是你开始调查的一些方法。
#5
0
I just read about some deadlock...hope this helps (search for control.leave in msdn.microwoft.com
我刚看到一些死锁……希望这能有所帮助。离开在msdn.microwoft.com
Caution Do not attempt to set focus from within the Enter, GotFocus, Leave, LostFocus, Validating, or Validated event handlers. Doing so can cause your application or the operating system to stop responding. For more information, see the WM_KILLFOCUS topic in the "Keyboard Input Reference" section, and the "Message Deadlocks" section of the "About Messages and Message Queues" topic in the MSDN library at http://msdn.microsoft.com/library.
注意,不要试图在Enter、GotFocus、Leave、LostFocus、验证或验证事件处理程序中设置焦点。这样做会导致应用程序或操作系统停止响应。有关更多信息,请参见MSDN库http://msdn.microsoft.com/library中的“键盘输入引用”部分中的WM_KILLFOCUS主题,以及“关于消息和消息队列”主题的“消息死锁”部分。
#6
0
You can try to track calls to the System.Window.UIElement.Focus method (in the PresentationCore.dll) in the context of your application using Runtime Flow (developed by me).
您可以尝试跟踪对system . windows . uielement的调用。使用运行时流(由我开发)在应用程序的上下文中集中方法(在PresentationCore.dll中)。