Domanda

I'm am writing a WinJS app that deals with data on the clipboard. I see you can use

Windows.ApplicationModel.DataTransfer.Clipboard.addEventListener("contentchanged", function (event) {

to listen for changes to clipboard. But if your app is not currently in focus, you get the error message

WinRT information: The clipboard could not be accessed because the application is not in the foreground.

How do you detect if the app is currently in focus? Also, is there an event or method to detect when it comes back into focus? For instance, say I set an internal variable that the clipboard changed when the app lost focus, and should check the content on return?

È stato utile?

Soluzione

The window.onblur event will tell you when the app loses focus; window.onfocus will tell you when it has the focus again. (There also the visibilitychange event on window/document and their visibilityState property that you can use to know if the app is visible but without the focus, as when sharing the screen.)

Scenario 4 of the Clipboard app sample in the Windows SDK shows how to use blur and focus events to manage calling the clipboard's getContent method at appropriate times.

Note also that because the contentchanged event comes from a WinRT object, be sure to removeEventListener if you're not going to be listening to the event for the entire lifetime of the app. That is, if you might possibly call addEventListener for this event more than once, neglecting to remove the listener will cause a memory leak.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top