您可以按照here 的建议使用Windows.Phone.UI.Input.HardwareButtons.BackPressed 事件。
private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e) {
if (checkbox.IsChecked) {
checkbox.IsChecked = false;
e.Handled = true;
}
}
问题似乎是重复的。
更新:
由于您使用的是 NavigationHelper 类,因此该类处理 BackPressed 事件并自行执行导航:
/// <summary>
/// Invoked when the hardware back button is pressed. For Windows Phone only.
/// </summary>
/// <param name="sender">Instance that triggered the event.</param>
/// <param name="e">Event data describing the conditions that led to the event.</param>
private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e) {
if (this.GoBackCommand.CanExecute(null)) {
e.Handled = true;
this.GoBackCommand.Execute(null);
}
}
因此在这种情况下,将 e.Handled 设置为 true 无效。为了控制导航,您可以编辑 NavigationHelper 类(它存在于项目的“Common”目录中)。
首先,将类的那部分替换为:
public event EventHandler<Windows.Phone.UI.Input.BackPressedEventArgs> BackPressed;
private void OnBackPressed(Windows.Phone.UI.Input.BackPressedEventArgs e) {
if (this.BackPressed != null) {
this.BackPressed(this, e);
}
}
/// <summary>
/// Invoked when the hardware back button is pressed. For Windows Phone only.
/// </summary>
/// <param name="sender">Instance that triggered the event.</param>
/// <param name="e">Event data describing the conditions that led to the event.</param>
private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e) {
this.OnBackPressed(e);
if (!e.Handled) {
if (this.GoBackCommand.CanExecute(null)) {
e.Handled = true;
this.GoBackCommand.Execute(null);
}
}
}
然后在你的页面中使用 NavigationHelper 类的新定义的 BackPressed 事件:
private NavigationHelper navigationHelper;
public RechargeAccount()
{
this.InitializeComponent();
this.navigationHelper = new NavigationHelper(this);
this.navigationHelper.LoadState += this.NavigationHelper_LoadState;
this.navigationHelper.SaveState += this.NavigationHelper_SaveState;
this.navigationHelper.BackPressed += this.NavigationHelper_BackPressed;
}
void NavigationHelper_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
if (RechargeAccountPivot.SelectedIndex == 2 && ePayBorder.Visibility == Windows.UI.Xaml.Visibility.Visible)
{
ePayBorder.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
e.Handled = true;
}
}