Search code examples
c#uwpmvvmcross

Accessing the close button on a window UWP C#


I have done some researching and I haven't found much that is useful for UWP. When the 'X' is hit on the window I need to call a function before the application actually closes. I can't seem to find any sources that deal with MVVM Cross and UWP. It seems that there may be a way but there is just simply a lack of resources online. I am really just looking to be pointed in the right direction.


Solution

  • When you create a Blank App and go to App.xaml.cs you should see an event in the end of the file OnSuspending

    See Below

    /// <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();
    }
    

    This is what you are looking for.