locked
How to catch close button on top right of the app when clicked by mouse in my windows 8.1 app?

    Question

  • When I use a mouse over my app , a bar of commands appear on top with minimize and close buttons.

    How can I catch in code behind (C#) that this close button was clicked ?

    When user clicks Alt F4 OnSuspending function in App,xaml.cs gets called. But for the close button it doesn't.

    I want to do some stuff before closing the app , so I need to find what it calls on code so I process it before terminating it.

    Monday, August 4, 2014 11:40 AM

Answers

  • Hi Mahagh,

    Sorry there is no specific event that can be fired when the user close the app, but you can try something like below:

    Register a event on VisibilityChanged event, for instance save the data to somewhere if the new windows size is null and also do something on suspending event to determine if the app is about to suspend but not terminate.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, August 7, 2014 8:03 AM
    Moderator

All replies

  • There isn't any special event that occurs when the application is closed besides the Suspending event: http://stackoverflow.com/questions/16168030/save-data-when-windows-8-store-app-closes.

    This means that you could do your stuff in the Suspending event handler in App.xaml.cs:

    sealed partial class App : Application
        {
            /// <summary>
            /// Initializes the singleton application object.  This is the first line of authored code
            /// executed, and as such is the logical equivalent of main() or WinMain().
            /// </summary>
            public App()
            {
                this.InitializeComponent();
                this.Suspending += OnSuspending;
    
                
            }
    
            ...
    
            /// <summary>
            /// Invoked when application execution is being suspended.  Application state is saved
            /// without knowing whether the application will be terminated or resumed with the contents
            /// of memory still intact.
            /// </summary>
            /// <param name="sender">The source of the suspend request.</param>
            /// <param name="e">Details about the suspend request.</param>
            private void OnSuspending(object sender, SuspendingEventArgs e)
            {
                var deferral = e.SuspendingOperation.GetDeferral();
                //TODO: Save application state and stop any background activity
                deferral.Complete();
            }
        }
    

    Monday, August 4, 2014 11:51 AM
  • Hi Mahagh,

    Sorry there is no specific event that can be fired when the user close the app, but you can try something like below:

    Register a event on VisibilityChanged event, for instance save the data to somewhere if the new windows size is null and also do something on suspending event to determine if the app is about to suspend but not terminate.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, August 7, 2014 8:03 AM
    Moderator