2013-05-05 89 views
4

我有代码看起来像这样:在WPF中,OnLostKeyboardFocus保证在OnGotKeyboardFocus之后被调用?

class MyUserControl : Control { ... 
    protected override void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e) 
    { 
     base.OnGotKeyboardFocus(e); 

     StartDoingSomethingRisky() 
    } 

    protected override void OnLostKeyboardFocus(KeyboardFocusChangedEventArgs e) 
    { 
     base.OnLostKeyboardFocus(e); 

     StopDoingSomethingRisky(); 
    } 

我应该担心,在某些情况下,我可能会继续的MyUserControl已经消失了即使在高风险的操作,或者已经覆盖我的案件100%?

回答

1

我唯一能看到的就是这个事实,就是你没有检查哪个元素“失去”了焦点。

http://msdn.microsoft.com/en-us/library/system.windows.uielement.lostkeyboardfocus.aspx: 由于此事件使用冒泡路由,失去焦点可能是一个子元素,而不是在事件处理程序实际上是连接的元素的元素。检查事件数据中的Source以确定失去焦点的实际元素。

如果您在某个特定元素获得焦点时发生了某些事情,并在焦点丢失时停止发生,那么您需要检查传入的参数以查看所谓的事件。

希望帮助

相关问题