如何以编程方式退出或关闭 UWP C++应用?

How to exit or close an UWP C++ app programmatically?

本文关键字:UWP C++ 应用 编程 方式 退出      更新时间:2023-10-16

我想在我的应用程序中添加"退出"按钮。
如果我写这样的东西:

void LibraryUWP::MainPage::ExitEvent(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
Application::Exit;
}

我有一个错误:

1>...\mainpage.xaml.cpp(32): 错误 C3867: 'Windows::UI::Xaml::Application::Exit':非标准语法;使用"&"创建指向成员的指针

如果我跑

void LibraryUWP::MainPage::ExitEvent(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
Application::Exit();
}

我有:

1>...\mainpage.xaml.cpp(32):错误 C2352:"Windows::UI::Xaml::应用程序::退出":非法调用非静态成员函数

如果我运行这个:

void LibraryUWP::MainPage::ExitEvent(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
Application::Current::Exit();
}

我有一个错误:

1>...\mainpage.xaml.cpp(32):错误 C2039:"退出":不是"Windows::UI::Xaml::应用程序::当前">
的成员1>...\mainpage.xaml.cpp(32):错误 C3861:"退出":找不到标识符

我知道,第一个和第二个变体不起作用。但是第三个呢?还是别的什么?

MS Visual Studio 2015 Update 3. 对于 Windows 10.10240 平台

附言如果我在没有最后一个护腕的情况下运行第三个变体,我有同样的错误,告诉我,我们没有这样的方法。

将代码更改为以下内容:

void MyApp::MainPage::Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
Application::Current->Exit();
}

谢谢

Stefan Wick - Windows Developer Platform

如果您的应用只有一个窗口,您可以像这样退出应用:

void UWPcpp::MainPage::Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
ApplicationView::GetForCurrentView()->TryConsolidateAsync();
}

这会暂停您的应用程序,而不是突然退出它,这也是重新启动应用程序时的唯一方法,它将具有与以前相同的大小和位置。