使用 C# 为 Windows Phone 8.1 使用 MessageDialog 关闭 App Confirmation
Close App Confirmation using MessageDialog for Windows Phone 8.1 using C#
我遇到了 Windows.UI.Popups.MessageDialog class 的问题。问题是:
我想询问用户是否要在单击后退按钮时关闭应用程序(在第一个 页面 上)。我尝试了两种方法:
首先是设置 BackPressed 方法 async:
private async void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
Windows.UI.Popups.MessageDialog logout = new Windows.UI.Popups.MessageDialog("Do You want to close the app?");
logout.Commands.Add(new Windows.UI.Popups.UICommand("Yes", Exit));
logout.Commands.Add(new Windows.UI.Popups.UICommand("No"));
await logout.ShowAsync();
}
第二种是使用 System.Threading.Tasks.Task.WaitAny(...) 方法:
private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
Windows.UI.Popups.MessageDialog logout = new Windows.UI.Popups.MessageDialog("Do You want to close the app?");
logout.Commands.Add(new Windows.UI.Popups.UICommand("Yes", Exit));
logout.Commands.Add(new Windows.UI.Popups.UICommand("No"));
System.Threading.Tasks.Task.WaitAny(logout.ShowAsync().AsTask());
}
我得到的是:
首先:MessageDialog 只显示一秒钟,然后 app 最小化,切换到它,然后按后退按钮,再次最小化 app,但是 MessageDialog 没有显示)。
第二:是死锁(点击"Yes"后,MessageDialog关闭,但没有调用Exit()方法,app卡死了) .
首先:
Microsoft 建议应用不要以编程方式自行关闭。
看这里:
App lifecycle - Windows app development
该页面显示:
You can't include any UI in your app to enable the user to close your app, or it won't pass the Store certification process.
虽然我知道商店中有一些应用程序可以执行此操作。
无论如何,在HardwareButtons_BackPressed
事件处理程序中,需要e.Handled = true;
来防止应用程序关闭。这也适用于在您的应用中向后导航时。
除此之外,Frame.CanGoBack
应检查应用程序是否应导航或关闭。
此外,您不需要使用任务,只需等待 MessageDialog.ShowAsync()
,尽管您可以创建一个任务,returns 一个布尔值告诉是否继续。
这是一个完整的代码示例,用于处理在帧堆栈中向后导航并显示一个消息对话框,询问在到达第一帧时是否关闭应用程序:
App.xaml.cs:
using Windows.Phone.UI.Input;
using Windows.UI.Popups;
public sealed partial class App : Application
{
public App()
{
HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}
private async void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
Frame frame = Window.Current.Content as Frame;
if (frame == null) { return; }
e.Handled = true;
if (frame.CanGoBack) { frame.GoBack(); return; }
else
{
string content = "Do you want to close the app?";
string title = "Close Confirmation";
MessageDialog confirmDialog = new MessageDialog(content, title);
confirmDialog.Commands.Add(new UICommand("Yes"));
confirmDialog.Commands.Add(new UICommand("No"));
var confirmResult = await confirmDialog.ShowAsync();
// "No" button pressed: Keep the app open.
if (confirmResult != null && confirmResult.Label == "No") { return; }
// "Back" or "Yes" button pressed: Close the app.
if (confirmResult == null || confirmResult.Label == "Yes") { Current.Exit(); }
}
}
}
我遇到了 Windows.UI.Popups.MessageDialog class 的问题。问题是:
我想询问用户是否要在单击后退按钮时关闭应用程序(在第一个 页面 上)。我尝试了两种方法:
首先是设置 BackPressed 方法 async:
private async void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
Windows.UI.Popups.MessageDialog logout = new Windows.UI.Popups.MessageDialog("Do You want to close the app?");
logout.Commands.Add(new Windows.UI.Popups.UICommand("Yes", Exit));
logout.Commands.Add(new Windows.UI.Popups.UICommand("No"));
await logout.ShowAsync();
}
第二种是使用 System.Threading.Tasks.Task.WaitAny(...) 方法:
private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
Windows.UI.Popups.MessageDialog logout = new Windows.UI.Popups.MessageDialog("Do You want to close the app?");
logout.Commands.Add(new Windows.UI.Popups.UICommand("Yes", Exit));
logout.Commands.Add(new Windows.UI.Popups.UICommand("No"));
System.Threading.Tasks.Task.WaitAny(logout.ShowAsync().AsTask());
}
我得到的是:
首先:MessageDialog 只显示一秒钟,然后 app 最小化,切换到它,然后按后退按钮,再次最小化 app,但是 MessageDialog 没有显示)。
第二:是死锁(点击"Yes"后,MessageDialog关闭,但没有调用Exit()方法,app卡死了) .
首先:
Microsoft 建议应用不要以编程方式自行关闭。
看这里:
App lifecycle - Windows app development
该页面显示:
You can't include any UI in your app to enable the user to close your app, or it won't pass the Store certification process.
虽然我知道商店中有一些应用程序可以执行此操作。
无论如何,在HardwareButtons_BackPressed
事件处理程序中,需要e.Handled = true;
来防止应用程序关闭。这也适用于在您的应用中向后导航时。
除此之外,Frame.CanGoBack
应检查应用程序是否应导航或关闭。
此外,您不需要使用任务,只需等待 MessageDialog.ShowAsync()
,尽管您可以创建一个任务,returns 一个布尔值告诉是否继续。
这是一个完整的代码示例,用于处理在帧堆栈中向后导航并显示一个消息对话框,询问在到达第一帧时是否关闭应用程序:
App.xaml.cs:
using Windows.Phone.UI.Input;
using Windows.UI.Popups;
public sealed partial class App : Application
{
public App()
{
HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}
private async void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
Frame frame = Window.Current.Content as Frame;
if (frame == null) { return; }
e.Handled = true;
if (frame.CanGoBack) { frame.GoBack(); return; }
else
{
string content = "Do you want to close the app?";
string title = "Close Confirmation";
MessageDialog confirmDialog = new MessageDialog(content, title);
confirmDialog.Commands.Add(new UICommand("Yes"));
confirmDialog.Commands.Add(new UICommand("No"));
var confirmResult = await confirmDialog.ShowAsync();
// "No" button pressed: Keep the app open.
if (confirmResult != null && confirmResult.Label == "No") { return; }
// "Back" or "Yes" button pressed: Close the app.
if (confirmResult == null || confirmResult.Label == "Yes") { Current.Exit(); }
}
}
}