[UWP]应用失败并重新获得焦点的事件? [英] [UWP]Event for when app loses and regains focus?

查看:65
本文介绍了[UWP]应用失败并重新获得焦点的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个winJS通用Windows 10应用程序。每当应用程序失去焦点时我想暂停视频,并在视频重新获得焦点时取消暂停。是否有我应该监听的事件?

I have created a winJS universal Windows 10 app. I would like to pause a video whenever the app loses focus and unpause when the video regains focus. Are there events that I should be listening for?

推荐答案

是的,您可以订阅Window.Current.Activated事件。我为C#代码道歉,但你应该能够在WinJS中做同样的事情。

Yes, you can subscribe to the Window.Current.Activated event. I apologise for the C# code, but you should be able to do the same in WinJS.

Window.Current.Activated += Current_Activated;




然后,检测事件处理程序中的状态:

Then, detect the state in the event handler:

void Current_Activated(object sender, Windows.UI.Core.WindowActivatedEventArgs e)
        {
            if (e.WindowActivationState ==
                Windows.UI.Core.CoreWindowActivationState.Deactivated)
            {
                FocusStatus.Text = "Focus lost!";

            }
            else if (e.WindowActivationState ==
                Windows.UI.Core.CoreWindowActivationState.PointerActivated)
            {
                FocusStatus.Text = "App has focus from clicking";
            }
            else if (e.WindowActivationState == Windows.UI.Core.CoreWindowActivationState.CodeActivated)
            {
                FocusStatus.Text = "App has focus from the OS";
            }
        }

我刚刚写了一篇关于这个主题的博客文章,内容更详细,包括检测应用程序的时间是可见/不可见的:

I just wrote a blog post about this topic with more detail, including detecting when the app is visible/invisible:

http://grogansoft.com/blog/?p=1269

http://grogansoft.com/blog/?p=1269


这篇关于[UWP]应用失败并重新获得焦点的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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