后退按钮在Windows Phone应用程序中向后导航到两页向后

时间:2015-06-16 18:44:05

标签: c# xaml windows-phone-8

在我的Windows手机应用程序中,虽然我只按了一次硬件后退按钮,但它有时会向后翻两页。 为了处理后退按钮,我使用了以下代码片段:

<td on="ItemConditionSelector()">
    @Model.InventoryExchangeItem.ReplacementProduct <br />
    @Model.InventoryExchangeItem.ReplacementItemNumber | @Model.InventoryExchangeItem.ReplacementProductSku | 
    @{
        var hideNew = @Model.InventoryExchangeItem.ReplacementItemCondition == "New" ? "hide" : "";
        var hideUsed = hideNew == "hide" ? "" : "hide";
    }
    <img id="NewIcon" class="@hideNew" src='~/Images/ico_NewMerchandise.png' />
    <img id="UsedIcon" class="@hideUsed" src='~/Images/ico_prelease.png' />

    @Model.InventoryExchangeItem.ReplacementItemCondition
</td>

private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e) { e.Handled = true; if (Frame.CanGoBack) Frame.GoBack(); } 方法中,我添加了以下行:

OnNavigatedTo()

如何确保按下后退按钮只能向后翻页一页?

1 个答案:

答案 0 :(得分:1)

OnNavigatedFrom方法中添加以下行:

Windows.Phone.UI.Input.HardwareButtons.BackPressed -= HardwareButtons_BackPressed;

如果您的XAML页面的部分类中不存在OnNavifatedFrom,请创建它,如下所示:

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
    Windows.Phone.UI.Input.HardwareButtons.BackPressed -= HardwareButtons_BackPressed;
}