简体   繁体   English

使用C#为Windows Phone 8.1使用MessageDialog关闭应用程序确认

[英]Close App Confirmation using MessageDialog for Windows Phone 8.1 using C#

I've got a problem with the Windows.UI.Popups.MessageDialog class. Windows.UI.Popups.MessageDialog类出现问题。 The thing is: 事情是:
I want to ask the user if (s) he wants to close the app, when clicking the back button (on the first Page ). 我想问用户在单击“后退”按钮(在第一页上 )时是否要关闭该应用程序。 I've tried two approaches: 我尝试了两种方法:
First is setting the BackPressed method async : 首先是将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();
}

Second is using the System.Threading.Tasks.Task.WaitAny(...) method: 其次是使用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());
}

What I get is: 我得到的是:
First: the MessageDialog shows just for a sec, then app minimizes, switching to it, and pressing the back button, just minimizes app again, but MessageDialog is not shown). 首先:MessageDialog仅显示一秒钟,然后应用程序最小化,切换到该对话框,然后按返回按钮,再次最小化应用程序,但不显示MessageDialog
Second: it's a deadlock (after clicking "Yes", MessageDialog closes, but the Exit() method is not called, app freezes). 第二:这是一个死锁(单击“是”后, MessageDialog关闭,但未调用Exit()方法,应用程序冻结)。

First of all: 首先:

Microsoft recommends that apps not close themselves programmatically. Microsoft建议应用程序不要以编程方式自行关闭。

See here: 看这里:

App lifecycle - Windows app development 应用程序生命周期-Windows应用程序开发

That page says: 该页面显示:

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. 您不能在应用程序中包含任何UI来使用户能够关闭您的应用程序,否则它将无法通过商店认证过程。

Although I know that there are apps in the Store that do this anyway. 尽管我知道应用商店中仍然有可以执行此操作的应用程序。


Anyway, in the HardwareButtons_BackPressed event handler, e.Handled = true; 无论如何,在HardwareButtons_BackPressed事件处理程序中, e.Handled = true; is needed to prevent the app from closing. 需要阻止应用程序关闭。 This also applies for when navigating backwards in your app. 在您的应用中向后导航时也是如此。

Besides this, Frame.CanGoBack should be checked to see if the app should navigate or close. 除此之外,应检查Frame.CanGoBack以查看应用程序是否应导航或关闭。

Also, you should not need to use a task, but just await MessageDialog.ShowAsync() , although you could create a task that returns a boolean telling whether to proceed or not. 同样,您可以不必使用任务,而只需等待MessageDialog.ShowAsync() ,尽管您可以创建一个返回布尔值的任务以告知是否继续执行任务。


Here is an entire code example to use that handles navigating backwards in the frame stack and displays a message dialog asking whether to close the app or not when the first frame is reached: 这是一个完整的代码示例,用于处理框架堆栈中的向后导航,并显示一个消息对话框,询问到达第一帧时是否关闭应用程序:

App.xaml.cs: 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(); }
        }
    }

}

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

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