A Microsoft platform for building and publishing apps for Windows devices.
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.