c# - 在 Windows Phone 8 中控制后退键

标签 c# windows-phone-8

在我按下返回键的应用程序中,我得到了按预期正常工作的消息框,显示消息框并且在后台应用程序正在运行,然后按确定/取消应用程序正常运行。

一旦消息框出现,我再次按下后退键,消息框应该消失,如果我再次按下后退键,它应该再次出现,除非我选择确定/取消。

我无法执行上述功能,在第二次按下后退按钮时,消息框没有关闭。

代码如下:

protected override void OnBackKeyPress(CancelEventArgs e)

{

    CustomMessageBox messageBox = new CustomMessageBox()
    {
         Caption = "Would you like to terminate the transfer?",
         //Message = "",
         LeftButtonContent = "Ok",
         RightButtonContent = "Cancel"
    };
    messageBox.show();

messageBox.Dismissed += (s1, e1) =>

{

switch (e1.Result)

{

            case CustomMessageBoxResult.LeftButton:
                //exiting the current transfer happening
                break;
            case CustomMessageBoxResult.RightButton:
                // do nothing here
                break;
            default:
                break;
        }
    };
messageBox.show();

}

现在的问题是每次返回键如下:

当前O/P: 反复按返回键,弹出消息框。

期望的 O/P:如果我按一次后退键,应该弹出消息框,如果我再按一次它应该在不干扰后台传输的情况下关闭,这应该在一次又一次地按后退键时发生,除非你选择一个选项来自消息框。

请帮助我控制消息框显示。

最佳答案

这是一个例子:

    protected override void OnBackKeyPress(CancelEventArgs e)
    {
        e.Cancel = true;

        MessageBox.Show("C#");
    }

基本实现:

bool OnOff = true;

        protected override void OnBackKeyPress(CancelEventArgs e)
        {
            base.OnBackKeyPress(e);

            e.Cancel = true;

            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                if (this.OnOff)
                {
                    CustomMessageBox message = new CustomMessageBox
                    {
                        Caption = "Would you like to terminate the transfer?",
                        LeftButtonContent = "Ok",
                        RightButtonContent = "Cancel"
                    };

                    message.Dismissed += (sender, args) =>
                    {
                        ((CustomMessageBox)sender).Dismissing += (o, eventArgs) => eventArgs.Cancel = true;

                        if (args.Result == CustomMessageBoxResult.LeftButton)
                        {
                            // Code
                        }
                        else if (args.Result == CustomMessageBoxResult.RightButton)
                        {
                            // Code
                        }
                    };

                    message.Show();

                    this.OnOff = false;
                }
                else
                    this.OnOff = true;

            });
        }

在这里您可以找到更多信息:Using the CustomMessageBox in OnBackKeyPressed

关于c# - 在 Windows Phone 8 中控制后退键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20469840/

相关文章:

c# - 如何从 RadioButtonList 中获取选中的值?

c# - 如何覆盖 Windows Phone 8 中的搜索按钮

c# - 使用 JSON.net 反序列化 JSON 数组

c - 如何将 native C代码用于Windows Phone运行时库?

windows-phone - WP8 阻止操作系统处理 LaunchApp NFC 标签读取

c# - WeakEventManager RemoveHandler 在异步调用时并不总是有效

C# 计算 xml 提要哈希的最佳方法是什么

c# - 将重复的选择合并为一个结果

c# - 在 ActiveMQ 中入队和出队时如何编写自定义日志

c# - 在 WP8 应用程序关闭后使用 BackgroundUploadAsync 将文件上传到 SkyDrive