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

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

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