Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I'm developing a metro app.

I'd like to know when my app is running in the background so that if a user completes an action that needs to be synced with a server, they can do so in offline mode and when the app is in the background I'd like to be able to try and sync say every minute, or detect when they are online.

I've tried to use:

C#
public HomeScreen()
   {
       this.InitializeComponent();

       Window.Current.VisibilityChanged += Current_VisibilityChanged;

   }

   void Current_VisibilityChanged(object sender, Windows.UI.Core.VisibilityChangedEventArgs e)
   {
       if (!e.Visible)
       {
           //Here you add the code that is executed when the app is not focused (when the focus is lost)
       }
   }


However this event seems to continuously fire when the app is the background. When the visibility (from an end users perspective) is not changing.

This wouldn't be too bad if there was an enumeration of states, such as; Backgound, Foreground. Etc.

I'm aware that you're able to query the visual state of the active window however this gives you information on the orientation of the windows, i.e. Snapped, Filled etc. There doesn't appear to be a way of telling if the form has lost focus (the user is interacting with another app/desktop) and then if that app is open before being Suspended.

Many thanks for any input.

Larry.
Posted
Comments
Sergey Alexandrovich Kryukov 26-Jul-13 14:00pm    
What exactly do you mean by "in the background"? There are several different meaning of this expression.
—SA
Laurence1234 26-Jul-13 19:33pm    
Hi SA,

I mean the app has not been suspended, it is not in snapped view or any other visual state, but another app is in focus.

The user is able to re-open that app by opening what I assume is the metro equivalent of the task bar, which is accessed in the top left hot corner.

Many thanks, Larry.
Sergey Alexandrovich Kryukov 26-Jul-13 21:40pm    
Okay, it means you only concern about Z-order of its main window. I would wonder why it may make any difference? The top-level window is the abstraction which is designed to logically isolate the behavior from other application windows...
—SA
Laurence1234 29-Jul-13 8:02am    
I think so. I think I'll have to make a ScreenCast to show what I mean.

Cheers,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900