我在Visual Studio 2013中创建了一个简单的Windows Phone 8.1应用程序,并添加了2个基本页面,以及在单击第一个屏幕上的按钮时导航到第二个屏幕的一些代码。转到第二个屏幕可以正常工作,但是当按下硬件后退按钮时,应用程序就会关闭。我希望应用程序在按下“返回”按钮时返回第一页。
我已按照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 :(得分: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