Answered by:
How can I know that my app gets in background.

Question
-
Hello,
Is there any way to know when user move away from my app. I need to stop some services when it navigate away from it.
Thanks in advance.
Monday, July 21, 2014 1:32 PM
Answers
-
Hi Zee,
place it in the OnLaunched-Method in the App.xaml before the Window.Current.Activate()-method is called:
public App() { this.InitializeComponent(); this.Suspending += OnSuspending; } /// <summary> /// Invoked when the application is launched normally by the end user. Other entry points /// will be used such as when the application is launched to open a specific file. /// </summary> /// <param name="e">Details about the launch request and process.</param> protected override void OnLaunched(LaunchActivatedEventArgs e) { #if DEBUG if (System.Diagnostics.Debugger.IsAttached) { this.DebugSettings.EnableFrameRateCounter = true; } #endif Frame rootFrame = Window.Current.Content as Frame; // Do not repeat app initialization when the Window already has content, // just ensure that the window is active if (rootFrame == null) { // Create a Frame to act as the navigation context and navigate to the first page rootFrame = new Frame(); // Set the default language rootFrame.Language = Windows.Globalization.ApplicationLanguages.Languages[0]; rootFrame.NavigationFailed += OnNavigationFailed; if (e.PreviousExecutionState == ApplicationExecutionState.Terminated) { //TODO: Load state from previously suspended application } // Place the frame in the current Window Window.Current.Content = rootFrame; } if (rootFrame.Content == null) { // When the navigation stack isn't restored navigate to the first page, // configuring the new page by passing required information as a navigation // parameter rootFrame.Navigate(typeof(MainPage), e.Arguments); } Window.Current.VisibilityChanged += (s, args) => { if (!args.Visible) { // Application went to background } else { // Application is FullScreen again } }; // Ensure the current window is active Window.Current.Activate(); }
Thomas Claudius Huber
"If you can't make your app run faster, make it at least look & feel extremly fast"
My latest app: The "Womanizer" :-)
twitter: @thomasclaudiush
homepage: www.thomasclaudiushuber.com
author of: ultimate Windows Store Apps handbook | ultimate WPF handbook | ultimate Silverlight handbook- Marked as answer by zee_patel Wednesday, July 23, 2014 5:13 AM
Tuesday, July 22, 2014 8:44 PM
All replies
-
Hi Zee,
you can use the VisibilityChanged-Event for that.
Find more here: http://msdn.microsoft.com/en-us/library/windows/apps/hh464925.aspx
Thomas Claudius Huber
"If you can't make your app run faster, make it at least look & feel extremly fast"
My latest app: The "Womanizer" :-)
twitter: @thomasclaudiush
homepage: www.thomasclaudiushuber.com
author of: ultimate Windows Store Apps handbook | ultimate WPF handbook | ultimate Silverlight handbookMonday, July 21, 2014 2:58 PM -
Hi Thomas
Thanks for your reply,
I already tried that code in App.xaml.cs but it is throwing ComException when app gets loaded. Here is the code.
public App() { this.InitializeComponent(); this.Suspending += OnSuspending; Window.Current.VisibilityChanged += (s, e) => { if (!e.Visible) { // Application went to background } else { // Application is FullScreen again } }; }
- Edited by zee_patel Tuesday, July 22, 2014 7:39 AM
Tuesday, July 22, 2014 7:39 AM -
Hi Zee,
place it in the OnLaunched-Method in the App.xaml before the Window.Current.Activate()-method is called:
public App() { this.InitializeComponent(); this.Suspending += OnSuspending; } /// <summary> /// Invoked when the application is launched normally by the end user. Other entry points /// will be used such as when the application is launched to open a specific file. /// </summary> /// <param name="e">Details about the launch request and process.</param> protected override void OnLaunched(LaunchActivatedEventArgs e) { #if DEBUG if (System.Diagnostics.Debugger.IsAttached) { this.DebugSettings.EnableFrameRateCounter = true; } #endif Frame rootFrame = Window.Current.Content as Frame; // Do not repeat app initialization when the Window already has content, // just ensure that the window is active if (rootFrame == null) { // Create a Frame to act as the navigation context and navigate to the first page rootFrame = new Frame(); // Set the default language rootFrame.Language = Windows.Globalization.ApplicationLanguages.Languages[0]; rootFrame.NavigationFailed += OnNavigationFailed; if (e.PreviousExecutionState == ApplicationExecutionState.Terminated) { //TODO: Load state from previously suspended application } // Place the frame in the current Window Window.Current.Content = rootFrame; } if (rootFrame.Content == null) { // When the navigation stack isn't restored navigate to the first page, // configuring the new page by passing required information as a navigation // parameter rootFrame.Navigate(typeof(MainPage), e.Arguments); } Window.Current.VisibilityChanged += (s, args) => { if (!args.Visible) { // Application went to background } else { // Application is FullScreen again } }; // Ensure the current window is active Window.Current.Activate(); }
Thomas Claudius Huber
"If you can't make your app run faster, make it at least look & feel extremly fast"
My latest app: The "Womanizer" :-)
twitter: @thomasclaudiush
homepage: www.thomasclaudiushuber.com
author of: ultimate Windows Store Apps handbook | ultimate WPF handbook | ultimate Silverlight handbook- Marked as answer by zee_patel Wednesday, July 23, 2014 5:13 AM
Tuesday, July 22, 2014 8:44 PM