得到一个通知,当焦点更改在Silverlight 4? [英] Getting a notification when focus changes in Silverlight 4?

查看:166
本文介绍了得到一个通知,当焦点更改在Silverlight 4?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想缺少调试的一些问题/额外的制表位。是否有某种全球性的事件,我可以附加到,这样我就可以登录该元素得到了集中每当更改焦点?谢谢!下面是我在做什么,现在,它工作得很好,但我仍然好奇,是否有另一种方式:

  DispatcherTimer定时器=新DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(0.2);
timer.Tick + = onTick;
timer.Start();

// ...

私有对象LastFocusedElement;
私人无效onTick(对象发件人,EventArgs的)
{
    对象ELEM = FocusManager.GetFocusedElement();
    如果(LastFocusedElement!= ELEM)
    {
        LastFocusedElement = ELEM;
        System.Diagnostics.Debug.WriteLine(+++ FOCUS +++焦点改为:+ ELEM +(ELEM == NULL:(+ elem.GetType()名+)));
    }
}
 

解决方案

您应该可以订阅的GotFocus 事件的最顶层容器。我没有看到任何处理的标志 RoutedEventArgs 所以,据我所知道的,它应该总是达不到它

 <用户控件...
             的GotFocus =UserControl_GotFocus>
    <! - 很多嵌套控件 - >
< /用户控件>

私人无效UserControl_GotFocus(对象发件人,RoutedEventArgs E)
{
    对象ELEM = e.OriginalSource;
    System.Diagnostics.Debug.WriteLine(+++ FOCUS +++焦点改为:+ ELEM +(ELEM == NULL:(+ elem.GetType()名+)));
}
 

I'm trying to debug some issues with missing/extra tab stops. Is there some kind of global event that I can attach to so that I can log which element got focus whenever focus changes? Thanks! Here's what I'm doing right now, which works well enough, but I'm still curious as to whether there's another way:

DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(0.2);
timer.Tick += onTick;
timer.Start();

// ...

private object LastFocusedElement;
private void onTick(object sender, EventArgs e)
{
    object elem = FocusManager.GetFocusedElement();
    if(LastFocusedElement != elem)
    {
        LastFocusedElement = elem;
        System.Diagnostics.Debug.WriteLine("+++FOCUS+++ Focus changed to: " + elem + (elem == null ? "" : " (" + elem.GetType().Name + ")"));
    }
}

解决方案

You should be able to subscribe to the GotFocus event for the "top-most" container. I don't see any Handled flag for RoutedEventArgs so as far as I can tell, it should always reach it

<UserControl ...
             GotFocus="UserControl_GotFocus">
    <!-- Lots of Nested Controls -->
</UserControl>

private void UserControl_GotFocus(object sender, RoutedEventArgs e)
{
    object elem = e.OriginalSource;
    System.Diagnostics.Debug.WriteLine("+++FOCUS+++ Focus changed to: " + elem + (elem == null ? "" : " (" + elem.GetType().Name + ")"));
}

这篇关于得到一个通知,当焦点更改在Silverlight 4?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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