PageRenderTime 59ms CodeModel.GetById 38ms RepoModel.GetById 0ms app.codeStats 0ms

/WP7.1/Templates/VB/WPCloud.SQL.Mem/WindowsPhoneCloud.Phone/Pages/MainPage.xaml.vb

#
Visual Basic | 195 lines | 142 code | 34 blank | 19 comment | 0 complexity | 060dcbcb55342daaecba8020e1f80567 MD5 | raw file
  1. ' ----------------------------------------------------------------------------------
  2. ' Microsoft Developer & Platform Evangelism
  3. '
  4. ' Copyright (c) Microsoft Corporation. All rights reserved.
  5. '
  6. ' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  7. ' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
  8. ' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  9. ' ----------------------------------------------------------------------------------
  10. ' The example companies, organizations, products, domain names,
  11. ' e-mail addresses, logos, people, places, and events depicted
  12. ' herein are fictitious. No association with any real company,
  13. ' organization, product, domain name, email address, logo, person,
  14. ' places, or events is intended or should be inferred.
  15. ' ----------------------------------------------------------------------------------
  16. Imports System.Windows.Controls
  17. Imports System.Windows.Navigation
  18. Imports Microsoft.Phone.Tasks
  19. Imports Microsoft.Samples.WindowsPhoneCloud.Phone.ViewModel
  20. Imports Microsoft.Samples.WindowsPhoneCloud.Phone.PivotContent
  21. Namespace Pages
  22. Partial Public Class MainPage
  23. Inherits PhoneApplicationPage
  24. Public Sub New()
  25. Me.InitializeComponent()
  26. Me.ViewModel = New MainPageViewModel()
  27. Me.LoadPivotItems()
  28. Me.OnMainPivotSelectionChanged()
  29. AddHandler Me.MainPivot.SelectionChanged, Sub(s, e) Me.OnMainPivotSelectionChanged()
  30. End Sub
  31. Public Property ViewModel() As MainPageViewModel
  32. Get
  33. Return TryCast(Me.DataContext, MainPageViewModel)
  34. End Get
  35. Set(ByVal value As MainPageViewModel)
  36. Me.DataContext = value
  37. End Set
  38. End Property
  39. Protected Overrides Sub OnBackKeyPress(ByVal e As System.ComponentModel.CancelEventArgs)
  40. SetApplicationState("UserBackPress", True)
  41. MyBase.OnBackKeyPress(e)
  42. End Sub
  43. Private Shared Function GetApplicationState(Of T)(ByVal key As String) As T
  44. If Not PhoneApplicationService.Current.State.ContainsKey(key) Then
  45. Return Nothing
  46. End If
  47. Return CType(PhoneApplicationService.Current.State(key), T)
  48. End Function
  49. Private Shared Sub RemoveApplicationState(ByVal key As String)
  50. If PhoneApplicationService.Current.State.ContainsKey(key) Then
  51. PhoneApplicationService.Current.State.Remove(key)
  52. End If
  53. End Sub
  54. Private Shared Sub SetApplicationState(ByVal key As String, ByVal value As Object)
  55. If PhoneApplicationService.Current.State.ContainsKey(key) Then
  56. PhoneApplicationService.Current.State.Remove(key)
  57. End If
  58. PhoneApplicationService.Current.State.Add(key, value)
  59. End Sub
  60. Private Sub LoadPivotItems()
  61. Me.MainPivot.Items.Clear()
  62. Dim notificationsPage = New NotificationsPage()
  63. Dim notificationsPivot = New PivotItem()
  64. notificationsPivot.Header = "notifications"
  65. notificationsPivot.Name = "PushNotifications"
  66. notificationsPivot.Content = notificationsPage
  67. notificationsPage.ViewModel = Me.ViewModel.NotificationsViewModel
  68. AddHandler notificationsPage.BeginPushConnection, AddressOf Me.OnBeginPushConnection
  69. AddHandler notificationsPage.EndPushConnection, AddressOf Me.OnEndPushConnection
  70. Me.MainPivot.Items.Add(notificationsPivot)
  71. Dim sqlSampleDataPage = New SqlSampleDataPage()
  72. Dim sqlSampleDataPivot = New PivotItem()
  73. sqlSampleDataPivot.Header = "sql azure data"
  74. sqlSampleDataPivot.Name = "SqlSampleData"
  75. sqlSampleDataPivot.Content = sqlSampleDataPage
  76. sqlSampleDataPage.ViewModel = Me.ViewModel.SqlSampleDataPageViewModel
  77. Me.MainPivot.Items.Add(sqlSampleDataPivot)
  78. Me.MainPivot.SelectedItem = notificationsPivot
  79. End Sub
  80. Private Function GetApplicationBarItemsByText(ByVal text As String) As IEnumerable(Of IApplicationBarMenuItem)
  81. Return Me.ApplicationBar.MenuItems.Cast(Of IApplicationBarMenuItem)() _
  82. .Where(Function(m) m.Text.Equals(text, StringComparison.OrdinalIgnoreCase)) _
  83. .Concat(
  84. Me.ApplicationBar.Buttons.Cast(Of IApplicationBarMenuItem)() _
  85. .Where(Function(b) b.Text.Equals(text, StringComparison.OrdinalIgnoreCase)))
  86. End Function
  87. Private Sub OnMainPivotSelectionChanged() Handles MainPivot.SelectionChanged
  88. Dim currentItem = TryCast(Me.MainPivot.SelectedItem, PivotItem)
  89. If currentItem IsNot Nothing Then
  90. Dim currentView = TryCast(currentItem.Content, UserControl)
  91. If currentView IsNot Nothing Then
  92. Dim viewModel = TryCast(currentView.DataContext, PivotItemViewModel)
  93. If viewModel IsNot Nothing Then
  94. viewModel.UpdateApplicationBarButtons(Me.ApplicationBar, {"log out"})
  95. End If
  96. End If
  97. End If
  98. End Sub
  99. Private Sub OnLaunchCamera(ByVal sender As Object, ByVal e As EventArgs)
  100. Dim cameraCaptureTask = New CameraCaptureTask()
  101. AddHandler cameraCaptureTask.Completed, AddressOf OnCameraCaptureTaskCompleted
  102. cameraCaptureTask.Show()
  103. End Sub
  104. Private Sub OnCameraCaptureTaskCompleted(ByVal sender As Object, ByVal e As PhotoResult)
  105. If e.TaskResult = TaskResult.OK Then
  106. SetApplicationState("PhotoResult", e)
  107. ' Delay navigation until the first navigated event.
  108. AddHandler Me.NavigationService.Navigated, AddressOf OnNavigatedCompleted
  109. End If
  110. End Sub
  111. Private Sub OnNavigatedCompleted(ByVal sender As Object, ByVal e As EventArgs)
  112. SetApplicationState("UserBackPress", false)
  113. ' Do the delayed navigation from the main page.
  114. Me.NavigationService.Navigate(New Uri("/Pages/UploadPhotoPage.xaml", UriKind.Relative))
  115. RemoveHandler Me.NavigationService.Navigated, AddressOf OnNavigatedCompleted
  116. End Sub
  117. Private Sub OnNavigatePage(ByVal sender As Object, ByVal e As NavigationEventArgs)
  118. Me.NavigationService.Navigate(e.Uri)
  119. End Sub
  120. Private Sub OnLogout(ByVal sender As Object, ByVal e As EventArgs)
  121. Me.MainPivot.IsEnabled = False
  122. Me.MainPivot.Opacity = 0.3R
  123. Me.LogOutStackPanel.Opacity = 1
  124. For Each button In Me.GetApplicationBarItemsByText("log out")
  125. button.IsEnabled = False
  126. Next button
  127. Dim pushNotificationClient = App.CloudClientFactory.ResolvePushNotificationClient()
  128. pushNotificationClient.Disconnect(Function(r) Me.Dispatcher.BeginInvoke(Sub() Me.CleanUp(r.Exception)))
  129. End Sub
  130. Private Sub CleanUp(ByVal exception As Exception)
  131. Me.MainPivot.IsEnabled = True
  132. Me.MainPivot.Opacity = 1
  133. Me.LogOutStackPanel.Opacity = 0
  134. For Each button In GetApplicationBarItemsByText("log out")
  135. button.IsEnabled = True
  136. Next button
  137. If exception IsNot Nothing Then
  138. MessageBox.Show(exception.Message, "Push Unregistration Error", MessageBoxButton.OK)
  139. End If
  140. ' Clean the current authentication token and view models.
  141. App.CloudClientFactory.CleanAuthenticationToken()
  142. Me.DataContext = Nothing
  143. Me.MainPivot.Items.Clear()
  144. SetApplicationState("UserBackPress", False)
  145. ' Navigate to the log in page.
  146. Me.NavigationService.GoBack()
  147. End Sub
  148. Private Sub OnBeginPushConnection(ByVal sender As Object, ByVal e As EventArgs)
  149. Dim items = Me.GetApplicationBarItemsByText("log out")
  150. For Each item In items
  151. item.IsEnabled = False
  152. Next item
  153. End Sub
  154. Private Sub OnEndPushConnection(ByVal sender As Object, ByVal e As EventArgs)
  155. Dim items = Me.GetApplicationBarItemsByText("log out")
  156. For Each item In items
  157. item.IsEnabled = True
  158. Next item
  159. End Sub
  160. End Class
  161. End Namespace