简体   繁体   English

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

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

I want to add "Exit" button in my app.我想在我的应用程序中添加“退出”按钮。
If I write something like this:如果我写这样的东西:

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

I have an error:我有一个错误:

1>...\\mainpage.xaml.cpp(32): error C3867: 'Windows::UI::Xaml::Application::Exit': non-standard syntax; 1>...\\mainpage.xaml.cpp(32): 错误 C3867: 'Windows::UI::Xaml::Application::Exit': 非标准语法; use '&' to create a pointer to member使用“&”创建指向成员的指针

If I run如果我跑

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

I have:我有:

1>...\\mainpage.xaml.cpp(32): error C2352: 'Windows::UI::Xaml::Application::Exit': illegal call of non-static member function 1>...\\mainpage.xaml.cpp(32): 错误 C2352: 'Windows::UI::Xaml::Application::Exit': 非静态成员函数的非法调用

And if I run this:如果我运行这个:

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

I have an error:我有一个错误:

1>...\\mainpage.xaml.cpp(32): error C2039: 'Exit': is not a member of 'Windows::UI::Xaml::Application::Current' 1>...\\mainpage.xaml.cpp(32): error C2039: 'Exit': 不是 'Windows::UI::Xaml::Application::Current' 的成员
1>...\\mainpage.xaml.cpp(32): error C3861: 'Exit': identifier not found 1>...\\mainpage.xaml.cpp(32):错误 C3861:“退出”:未找到标识符

I understand, that first & second variant cannot work.我明白,第一个和第二个变体不能工作。 But what about the third?但是第三个呢? Or something else?或者是其他东西?

MS Visual Studio 2015 Update 3. For Windows 10.10240 platform MS Visual Studio 2015 更新 3。适用于 Windows 10.10240 平台

PS If I run third variant without last bracers, I have the same error, that tells me, that we have no such method. PS如果我在没有最后一个护腕的情况下运行第三个变体,我会遇到同样的错误,这告诉我,我们没有这样的方法。

Change your code to this:将您的代码更改为:

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

Thanks,谢谢,

Stefan Wick - Windows Developer Platform Stefan Wick - Windows 开发人员平台

If your app only has one window, you can exit app like this:如果你的应用只有一个窗口,你可以像这样退出应用:

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

This suspends your app instead of abruptly exiting it, also this is the only way when you relaunch your app it will have the same size and location as previous.这会暂停您的应用程序而不是突然退出它,这也是您重新启动应用程序时它具有与以前相同的大小和位置的唯一方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM