Share via

WinRT application suspension clarification

Anonymous
2023-06-08T20:41:18.52+00:00

I recently posted this question which mainly works. The problem is my code gets run when I minimize the program and not when I Exit. Granted that minimize is more of a suspension than Exit is, but I found this and it says to follow the directions so suspension happens when I Exit.

I revisited that page and discovered I was missing the part where I handle the exit request, but since it is written in CS and I am writing mine in WinRT/C++, I am having difficulty in rewriting the code. Here is the code that has me stumped.

public MainPage()
{
     this.InitializeComponent();
     Windows.UI.Core.Preview.SystemNavigationManagerPreview.GetForCurrentView().CloseRequested +=
         async (sender, args) =>
         {
             args.Handled = true;
             var result = await SaveConfirm.ShowAsync();
             if (result == ContentDialogResult.Primary)
             {
                 // Save work;
             }
             else
             {
                 App.Current.Exit();
             }
         };
 }

So I need to put this in my MainPage.cpp. I get that CloseRequested() is just a listener for the close button. I am struggling with the next line that is an async() and the +=. (I sort of understand the meaning of this code, but WinRT has no equivalent).

The rest of the lines of code I get as I have used a ContentDialog before. I am not sure how to make this work for WinRT/C++. Thanks for your help.

Developer technologies | Universal Windows Platform (UWP)
Developer technologies | C++
Developer technologies | C++

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.


1 answer

Sort by: Most helpful
  1. Junjie Zhu - MSFT 21,746 Reputation points
    2023-06-09T05:55:53.9+00:00

    Hello @Anonymous ,

    Welcome to Microsoft Q&A!

    You can refer to the following code.

    //this is in pch.h
    #include <winrt/Windows.UI.Core.Preview.h>
    
    //this is in page header file
    Windows::Foundation::IAsyncAction OnCloseRequested(Windows::Foundation::IInspectable sender, Windows::UI::Core::Preview::SystemNavigationCloseRequestedPreviewEventArgs e);
    
    using namespace Windows::UI::Core::Preview;
    using namespace Windows::UI::Xaml::Controls;
    
    void MainPage::ClickHandler(IInspectable const&, RoutedEventArgs const&)
    {
        myButton().Content(box_value(L"Clicked"));
    
        auto previewWnd = Windows::UI::Core::Preview::SystemNavigationManagerPreview::GetForCurrentView();
        previewWnd.CloseRequested({ this, &MainPage::OnCloseRequested });
      
    }
    
    Windows::Foundation::IAsyncAction MainPage::OnCloseRequested(IInspectable sender, SystemNavigationCloseRequestedPreviewEventArgs e)
    {
        e.Handled(true);
    
        ContentDialog dialog;
        dialog.Title(winrt::box_value(L"Title"));
        dialog.Content(winrt::box_value(L"Content"));
        dialog.PrimaryButtonText(L"OK");
    
        auto result= co_await dialog.ShowAsync();
    
        if (result == ContentDialogResult::Primary)
        {
            // Save work;
        }
        else
        {
            Application().Current().Exit();
        }
            
    }
    

    Don't forget to add Capability confirmAppClose.

    <Package ....
    xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
    ....IgnorableNamespaces="uap mp build rescap">
    
      <Capabilities>
        <Capability Name="internetClient" />
    	<rescap:Capability Name="confirmAppClose" />
      </Capabilities
    
    

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.