PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Visual Basic | 231 lines | 173 code | 39 blank | 19 comment | 0 complexity | d45789e7bdf90b9153e5ea66cc3421ea 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 OnBeginPushConnection
  69. AddHandler notificationsPage.EndPushConnection, AddressOf Me.OnEndPushConnection
  70. Me.MainPivot.Items.Add(notificationsPivot)
  71. Dim tablesPage = New TablesPage()
  72. Dim tablesPivot = New PivotItem()
  73. tablesPivot.Header = "tables list"
  74. tablesPivot.Name = "TablesList"
  75. tablesPivot.Content = tablesPage
  76. tablesPage.ViewModel = Me.ViewModel.TablesPageViewModel
  77. Me.MainPivot.Items.Add(tablesPivot)
  78. Dim sampleDataTablesPage = New SampleDataTablePage()
  79. Dim sampleDataTablesPivot = New PivotItem()
  80. sampleDataTablesPivot.Header = "sample data"
  81. sampleDataTablesPivot.Name = "SampleDataRows"
  82. sampleDataTablesPivot.Content = sampleDataTablesPage
  83. sampleDataTablesPage.ViewModel = Me.ViewModel.SampleDataTablePageViewModel
  84. AddHandler sampleDataTablesPage.Navigate, AddressOf Me.OnNavigatePage
  85. Me.MainPivot.Items.Add(sampleDataTablesPivot)
  86. Dim listBlobsPage = New ListBlobsPage()
  87. Dim listBlobsPivot = New PivotItem()
  88. listBlobsPivot.Header = "list blobs"
  89. listBlobsPivot.Name = "ListBlobs"
  90. listBlobsPivot.Content = listBlobsPage
  91. listBlobsPage.ViewModel = Me.ViewModel.ListBlobsPageViewModel
  92. AddHandler listBlobsPage.TakePhoto, AddressOf Me.OnLaunchCamera
  93. Me.MainPivot.Items.Add(listBlobsPivot)
  94. Dim listQueuesPage = New ListQueuesPage()
  95. Dim listQueuesPivot = New PivotItem()
  96. listQueuesPivot.Header = "queues"
  97. listQueuesPivot.Name = "ListQueuesPage"
  98. listQueuesPivot.Content = listQueuesPage
  99. listQueuesPage.ViewModel = Me.ViewModel.ListQueuesPageViewModel
  100. AddHandler listQueuesPage.Navigate, AddressOf Me.OnNavigatePage
  101. Me.MainPivot.Items.Add(listQueuesPivot)
  102. Dim sqlSampleDataPage = New SqlSampleDataPage()
  103. Dim sqlSampleDataPivot = New PivotItem()
  104. sqlSampleDataPivot.Header = "sql azure data"
  105. sqlSampleDataPivot.Name = "SqlSampleData"
  106. sqlSampleDataPivot.Content = sqlSampleDataPage
  107. sqlSampleDataPage.ViewModel = Me.ViewModel.SqlSampleDataPageViewModel
  108. Me.MainPivot.Items.Add(sqlSampleDataPivot)
  109. Me.MainPivot.SelectedItem = notificationsPivot
  110. End Sub
  111. Private Function GetApplicationBarItemsByText(ByVal text As String) As IEnumerable(Of IApplicationBarMenuItem)
  112. Return Me.ApplicationBar.MenuItems.Cast(Of IApplicationBarMenuItem)() _
  113. .Where(Function(m) m.Text.Equals(text, StringComparison.OrdinalIgnoreCase)) _
  114. .Concat(
  115. Me.ApplicationBar.Buttons.Cast(Of IApplicationBarMenuItem)() _
  116. .Where(Function(b) b.Text.Equals(text, StringComparison.OrdinalIgnoreCase)))
  117. End Function
  118. Private Sub OnMainPivotSelectionChanged() Handles MainPivot.SelectionChanged
  119. Dim currentItem = TryCast(Me.MainPivot.SelectedItem, PivotItem)
  120. If currentItem IsNot Nothing Then
  121. Dim currentView = TryCast(currentItem.Content, UserControl)
  122. If currentView IsNot Nothing Then
  123. Dim viewModel = TryCast(currentView.DataContext, PivotItemViewModel)
  124. If viewModel IsNot Nothing Then
  125. viewModel.UpdateApplicationBarButtons(Me.ApplicationBar, {"log out"})
  126. End If
  127. End If
  128. End If
  129. End Sub
  130. Private Sub OnLaunchCamera(ByVal sender As Object, ByVal e As EventArgs)
  131. Dim cameraCaptureTask = New CameraCaptureTask()
  132. AddHandler cameraCaptureTask.Completed, AddressOf OnCameraCaptureTaskCompleted
  133. cameraCaptureTask.Show()
  134. End Sub
  135. Private Sub OnCameraCaptureTaskCompleted(ByVal sender As Object, ByVal e As PhotoResult)
  136. If e.TaskResult = TaskResult.OK Then
  137. SetApplicationState("PhotoResult", e)
  138. ' Delay navigation until the first navigated event.
  139. AddHandler Me.NavigationService.Navigated, AddressOf OnNavigatedCompleted
  140. End If
  141. End Sub
  142. Private Sub OnNavigatedCompleted(ByVal sender As Object, ByVal e As EventArgs)
  143. SetApplicationState("UserBackPress", False)
  144. ' Do the delayed navigation from the main page.
  145. Me.NavigationService.Navigate(New Uri("/Pages/UploadPhotoPage.xaml", UriKind.Relative))
  146. RemoveHandler Me.NavigationService.Navigated, AddressOf OnNavigatedCompleted
  147. End Sub
  148. Private Sub OnNavigatePage(ByVal sender As Object, ByVal e As NavigationEventArgs)
  149. Me.NavigationService.Navigate(e.Uri)
  150. End Sub
  151. Private Sub OnLogout(ByVal sender As Object, ByVal e As EventArgs)
  152. Me.MainPivot.IsEnabled = False
  153. Me.MainPivot.Opacity = 0.3R
  154. Me.LogOutStackPanel.Opacity = 1
  155. For Each button In Me.GetApplicationBarItemsByText("log out")
  156. button.IsEnabled = False
  157. Next button
  158. Dim pushNotificationClient = App.CloudClientFactory.ResolvePushNotificationClient()
  159. pushNotificationClient.Disconnect(Function(r) Me.Dispatcher.BeginInvoke(Sub() Me.CleanUp(r.Exception)))
  160. End Sub
  161. Private Sub CleanUp(ByVal exception As Exception)
  162. Me.MainPivot.IsEnabled = True
  163. Me.MainPivot.Opacity = 1
  164. Me.LogOutStackPanel.Opacity = 0
  165. For Each button In GetApplicationBarItemsByText("log out")
  166. button.IsEnabled = True
  167. Next button
  168. If exception IsNot Nothing Then
  169. MessageBox.Show(exception.Message, "Push Unregistration Error", MessageBoxButton.OK)
  170. End If
  171. ' Clean the current authentication token and view models.
  172. App.CloudClientFactory.CleanAuthenticationToken()
  173. Me.DataContext = Nothing
  174. Me.MainPivot.Items.Clear()
  175. SetApplicationState("UserBackPress", False)
  176. ' Navigate to the log in page.
  177. Me.NavigationService.GoBack()
  178. End Sub
  179. Private Sub OnBeginPushConnection(ByVal sender As Object, ByVal e As EventArgs)
  180. Dim items = Me.GetApplicationBarItemsByText("log out")
  181. For Each item In items
  182. item.IsEnabled = False
  183. Next item
  184. End Sub
  185. Private Sub OnEndPushConnection(ByVal sender As Object, ByVal e As EventArgs)
  186. Dim items = Me.GetApplicationBarItemsByText("log out")
  187. For Each item In items
  188. item.IsEnabled = True
  189. Next item
  190. End Sub
  191. End Class
  192. End Namespace