在我按下返回键的应用程序中,我得到了按预期正常工作的消息框,显示消息框并且在后台应用程序正在运行,然后按确定/取消应用程序正常运行。
一旦消息框出现,我再次按下后退键,消息框应该消失,如果我再次按下后退键,它应该再次出现,除非我选择确定/取消。
我无法执行上述功能,在第二次按下后退按钮时,消息框没有关闭。
代码如下:
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/