10. 到此为止第二个功能页面就完全定义好了。我们把它们和第一个页面上的返回事件处理函数和Next按钮click处理函数绑定到一起。在第一个页面的代码文件ContactDetailPage1.xaml.cs里添加如下代码:
' On clicking the next button on PF1, navigate to PF2
Private Sub OnNextClick(ByVal sender As Object, _
ByVal e As RoutedEventArgs)
Dim pageFunction As ContactDetailPage2 = New ContactDetailPage2
AddHandler pageFunction.Return, AddressOf pageFunction2_Return
System.Windows.Navigation.NavigationService.GetNavigationService(Me).Navigate(pageFunction)
End Sub
' When the second PF returns, get the Contact object it
' sent back and add the first PF’s data to it
' Then return to the UILessPageFunction that called this
' ContactDetailPage1 PF.
Sub pageFunction2_Return(ByVal sender As Object, _
ByVal e As ReturnEventArgs(Of Object))
Dim c As Contact = CType(e.Result, Contact)
c.FirstName = Me.txtFirstName.Text
c.LastName = Me.txtLastName.Text
c.EmailAddress = Me.txtEmailAddress.Text
Try
c.HomePage = New Uri(Me.txtHomePage.Text)
Catch generatedExceptionVariable0 As UriFormatException
c.HomePage = Nothing
End Try
OnReturn(New ReturnEventArgs(Of Object)(c))
End Sub
Private Sub OnNextClick(ByVal sender As Object, _
ByVal e As RoutedEventArgs)
Dim pageFunction As ContactDetailPage2 = New ContactDetailPage2
AddHandler pageFunction.Return, AddressOf pageFunction2_Return
System.Windows.Navigation.NavigationService.GetNavigationService(Me).Navigate(pageFunction)
End Sub
' When the second PF returns, get the Contact object it
' sent back and add the first PF’s data to it
' Then return to the UILessPageFunction that called this
' ContactDetailPage1 PF.
Sub pageFunction2_Return(ByVal sender As Object, _
ByVal e As ReturnEventArgs(Of Object))
Dim c As Contact = CType(e.Result, Contact)
c.FirstName = Me.txtFirstName.Text
c.LastName = Me.txtLastName.Text
c.EmailAddress = Me.txtEmailAddress.Text
Try
c.HomePage = New Uri(Me.txtHomePage.Text)
Catch generatedExceptionVariable0 As UriFormatException
c.HomePage = Nothing
End Try
OnReturn(New ReturnEventArgs(Of Object)(c))
End Sub
11. 您的应用程序就差两步就完成了!当联系人列表当中的某一项被选定的时候,我们要把联系人详细信息显示在右边的面板。下面我么就来写这一段代码。我们不是曾经在MainWindow.xaml.cs里面创建了一个空的ListItemSelected方法吗?现在我们把它实现吧:
' Triggered when an item in the Contacts list is selected
Sub ListItemSelected(ByVal sender As Object, _
ByVal e As SelectionChangedEventArgs)
' show first page function on the right hand side frame
Dim pageFunction As ContactDetailPage1 = _
New ContactDetailPage1(False, allContacts.SelectedIndex)
AddHandler pageFunction.Return, AddressOf pageFunction0_Return
Me.Frame_RightPane.Navigate(pageFunction)
' update status bar
Dim cl As ContactList = _
CType(Application.Current.Properties("ContactList"), _
ContactList)
Dim c As Contact = cl(allContacts.SelectedIndex)
Me.tb.Text = c.FirstName + " " + c.LastName
End Sub
Sub ListItemSelected(ByVal sender As Object, _
ByVal e As SelectionChangedEventArgs)
' show first page function on the right hand side frame
Dim pageFunction As ContactDetailPage1 = _
New ContactDetailPage1(False, allContacts.SelectedIndex)
AddHandler pageFunction.Return, AddressOf pageFunction0_Return
Me.Frame_RightPane.Navigate(pageFunction)
' update status bar
Dim cl As ContactList = _
CType(Application.Current.Properties("ContactList"), _
ContactList)
Dim c As Contact = cl(allContacts.SelectedIndex)
Me.tb.Text = c.FirstName + " " + c.LastName
End Sub
因为ContactDetailPage1对话框是以只读模式显示的,它不应该返回任何值。我们就在MainWindow.xaml.cs里创建一个空的pageFunction0_Return,好让程序能顺利编译。
12. 构建并运行您的应用程序。地址簿应用程序的联系人应该能够显示出来了:
' This is the ContactDetailPage1 page function's return handler.
' Merely a placeholder since the page is displayed in read mode and
' doesn't return anything.
Sub pageFunction0_Return(ByVal sender As Object, _
ByVal e As System.Windows.Navigation.ReturnEventArgs(Of Object))
'place holder. no logic needed
End Sub
' Merely a placeholder since the page is displayed in read mode and
' doesn't return anything.
Sub pageFunction0_Return(ByVal sender As Object, _
ByVal e As System.Windows.Navigation.ReturnEventArgs(Of Object))
'place holder. no logic needed
End Sub
如果您点击工具栏上的加号按钮,或者选择File ? Add Contact菜单项,或者在ListBox的右键菜单里选择Add New Contact,您可以看到Add Contact向导。