2015-06-20 76 views
0

我已經在Visual Studio 2013中創建了一個簡單的Windows Phone 8.1應用程序,並添加了2個基本頁面以及一些代碼以導航到第二個屏幕,第一個屏幕被點擊。進入第二個屏幕可以正常工作,但是當按下硬件後退按鈕時,應用程序會關閉。我希望應用程序在按下「後退」按鈕時返回到第一頁。返回按鈕關閉應用程序,而不是在Windows Phone 8.1上返回

我按照NavigationHelper.vb中的說明操作,並添加了帶有一些初始化命令的Sub New(),但後退按鈕仍然不起作用。我怎樣才能按回來正確工作?提前致謝。

這裏是我的代碼:

BasicPage1.xaml.vb

Imports App1.Common 

Public NotInheritable Class BasicPage1 
    Inherits Page 

    Private WithEvents _navigationHelper As New NavigationHelper(Me) 
    Private ReadOnly _defaultViewModel As New ObservableDictionary() 

    Public ReadOnly Property NavigationHelper As NavigationHelper 
     Get 
      Return _navigationHelper 
     End Get 
    End Property 

    Sub New() 

     InitializeComponent() 

     Me._navigationHelper = New Common.NavigationHelper(Me) 
     AddHandler Me._navigationHelper.LoadState, AddressOf NavigationHelper_LoadState 
     AddHandler Me._navigationHelper.SaveState, AddressOf NavigationHelper_SaveState 
    End Sub 

    Public ReadOnly Property DefaultViewModel As ObservableDictionary 
     Get 
      Return _defaultViewModel 
     End Get 
    End Property 

    Private Sub NavigationHelper_LoadState(sender As Object, e As LoadStateEventArgs) Handles _navigationHelper.LoadState 
    End Sub 

    Private Sub NavigationHelper_SaveState(sender As Object, e As SaveStateEventArgs) Handles _navigationHelper.SaveState 
    End Sub 

#Region "NavigationHelper registration" 

    Protected Overrides Sub OnNavigatedTo(e As NavigationEventArgs) 
     _navigationHelper.OnNavigatedTo(e) 
    End Sub 

    Protected Overrides Sub OnNavigatedFrom(e As NavigationEventArgs) 
     _navigationHelper.OnNavigatedFrom(e) 
    End Sub 

#End Region 

    Private Sub button_Click(sender As Object, e As RoutedEventArgs) Handles Button.Click 
     Frame.Navigate(GetType(BasicPage2)) 
    End Sub 
End Class 

BasicPage2.xaml.vb

Imports App1.Common 

Public NotInheritable Class BasicPage2 
    Inherits Page 

    Private WithEvents _navigationHelper As New NavigationHelper(Me) 
    Private ReadOnly _defaultViewModel As New ObservableDictionary() 

    Public ReadOnly Property NavigationHelper As NavigationHelper 
     Get 
      Return _navigationHelper 
     End Get 
    End Property 

    Sub New() 

     InitializeComponent() 

     Me._navigationHelper = New Common.NavigationHelper(Me) 
     AddHandler Me._navigationHelper.LoadState, AddressOf NavigationHelper_LoadState 
     AddHandler Me._navigationHelper.SaveState, AddressOf NavigationHelper_SaveState 
    End Sub 

    Public ReadOnly Property DefaultViewModel As ObservableDictionary 
     Get 
      Return _defaultViewModel 
     End Get 
    End Property 

    Private Sub NavigationHelper_LoadState(sender As Object, e As LoadStateEventArgs) Handles _navigationHelper.LoadState 
    End Sub 

    Private Sub NavigationHelper_SaveState(sender As Object, e As SaveStateEventArgs) Handles _navigationHelper.SaveState 
    End Sub 

#Region "NavigationHelper registration" 

    Protected Overrides Sub OnNavigatedTo(e As NavigationEventArgs) 
     _navigationHelper.OnNavigatedTo(e) 
    End Sub 

    Protected Overrides Sub OnNavigatedFrom(e As NavigationEventArgs) 
     _navigationHelper.OnNavigatedFrom(e) 
    End Sub 

#End Region 

End Class 

完整的解決方案是在這裏:https://github.com/OrangeFlash81/SimpleWPApp

回答

0

我通過增加一個固定自己的問題事件處理程序使用AddHandler並使用Frame.Navigate返回到第一頁。我加了這個代碼:

Sub New() 
    AddHandler HardwareButtons.BackPressed, AddressOf BackPressed 
End Sub 

Sub BackPressed(sender As Object, e As BackPressedEventArgs) 
    e.Handled = True 
    Frame.Navigate(GetType(BasicPage1)) 
End Sub 
相關問題