PageRenderTime 63ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Visual Basic | 195 lines | 144 code | 32 blank | 19 comment | 0 complexity | e8d173d67af10e4aa9953118a6ef4fac 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 Microsoft.Samples.WindowsPhoneCloud.Phone.ViewModel
  17. Imports Microsoft.Samples.WindowsPhoneCloud.Phone.PivotContent
  18. Imports Microsoft.Phone.Tasks
  19. Imports System.IO.IsolatedStorage
  20. Namespace Pages
  21. Partial Public Class MainPage
  22. Inherits PhoneApplicationPage
  23. Public Sub New()
  24. Me.InitializeComponent()
  25. Me.ViewModel = New MainPageViewModel()
  26. Me.LoadPivotItems()
  27. Me.OnMainPivotSelectionChanged()
  28. AddHandler Me.MainPivot.SelectionChanged, Sub(s, e) Me.OnMainPivotSelectionChanged()
  29. End Sub
  30. Public Property ViewModel() As MainPageViewModel
  31. Get
  32. Return TryCast(Me.DataContext, MainPageViewModel)
  33. End Get
  34. Set(ByVal value As MainPageViewModel)
  35. Me.DataContext = value
  36. End Set
  37. End Property
  38. Protected Overrides Sub OnBackKeyPress(ByVal e As System.ComponentModel.CancelEventArgs)
  39. SetApplicationState("UserBackPress", True)
  40. MyBase.OnBackKeyPress(e)
  41. End Sub
  42. Private Shared Function GetIsolatedStorageSetting(Of T)(ByVal key As String) As T
  43. Dim isolatedStorage As IDictionary(Of String, Object) = IsolatedStorageSettings.ApplicationSettings
  44. If Not isolatedStorage.ContainsKey(key) Then
  45. Return Nothing
  46. End If
  47. Return CType(isolatedStorage(key), T)
  48. End Function
  49. Private Shared Sub RemoveIsolatedStorageSetting(ByVal key As String)
  50. Dim isolatedStorage As IDictionary(Of String, Object) = IsolatedStorageSettings.ApplicationSettings
  51. If isolatedStorage.ContainsKey(key) Then
  52. isolatedStorage.Remove(key)
  53. End If
  54. End Sub
  55. Private Shared Sub SetApplicationState(ByVal key As String, ByVal value As Object)
  56. If PhoneApplicationService.Current.State.ContainsKey(key) Then
  57. PhoneApplicationService.Current.State.Remove(key)
  58. End If
  59. PhoneApplicationService.Current.State.Add(key, value)
  60. End Sub
  61. Private Sub LoadPivotItems()
  62. Me.MainPivot.Items.Clear()
  63. Dim notificationsPage = New NotificationsPage()
  64. Dim notificationsPivot = New PivotItem()
  65. notificationsPivot.Header = "notifications"
  66. notificationsPivot.Name = "PushNotifications"
  67. notificationsPivot.Content = notificationsPage
  68. notificationsPage.ViewModel = Me.ViewModel.NotificationsViewModel
  69. AddHandler notificationsPage.BeginPushConnection, AddressOf Me.OnBeginPushConnection
  70. AddHandler notificationsPage.EndPushConnection, AddressOf Me.OnEndPushConnection
  71. Me.MainPivot.Items.Add(notificationsPivot)
  72. Dim sqlSampleDataPage = New SqlSampleDataPage()
  73. Dim sqlSampleDataPivot = New PivotItem()
  74. sqlSampleDataPivot.Header = "sql azure data"
  75. sqlSampleDataPivot.Name = "SqlSampleData"
  76. sqlSampleDataPivot.Content = sqlSampleDataPage
  77. sqlSampleDataPage.ViewModel = Me.ViewModel.SqlSampleDataPageViewModel
  78. Me.MainPivot.Items.Add(sqlSampleDataPivot)
  79. Me.MainPivot.SelectedItem = notificationsPivot
  80. End Sub
  81. Private Function GetApplicationBarItemsByText(ByVal text As String) As IEnumerable(Of IApplicationBarMenuItem)
  82. 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)))
  83. End Function
  84. Private Sub OnMainPivotSelectionChanged()
  85. Dim currentItem = TryCast(Me.MainPivot.SelectedItem, PivotItem)
  86. If currentItem IsNot Nothing Then
  87. Dim currentView = TryCast(currentItem.Content, UserControl)
  88. If currentView IsNot Nothing Then
  89. Dim viewModel = TryCast(currentView.DataContext, PivotItemViewModel)
  90. If viewModel IsNot Nothing Then
  91. viewModel.UpdateApplicationBarButtons(Me.ApplicationBar, {"log out"})
  92. End If
  93. End If
  94. End If
  95. End Sub
  96. Private Sub OnLaunchCamera(ByVal sender As Object, ByVal e As EventArgs)
  97. Dim cameraCaptureTask = New CameraCaptureTask()
  98. AddHandler cameraCaptureTask.Completed, AddressOf OnCameraCaptureTaskCompleted
  99. cameraCaptureTask.Show()
  100. End Sub
  101. Private Sub OnCameraCaptureTaskCompleted(ByVal sender As Object, ByVal e As PhotoResult)
  102. If e.TaskResult = TaskResult.OK Then
  103. SetApplicationState("PhotoResult", e)
  104. ' Delay navigation until the first navigated event.
  105. AddHandler Me.NavigationService.Navigated, AddressOf OnNavigatedCompleted
  106. End If
  107. End Sub
  108. Private Sub OnNavigatedCompleted(ByVal sender As Object, ByVal e As EventArgs)
  109. SetApplicationState("UserBackPress", False)
  110. ' Do the delayed navigation from the main page.
  111. Me.NavigationService.Navigate(New Uri("/Pages/UploadPhotoPage.xaml", UriKind.Relative))
  112. RemoveHandler Me.NavigationService.Navigated, AddressOf OnNavigatedCompleted
  113. End Sub
  114. Private Sub OnNavigatePage(ByVal sender As Object, ByVal e As NavigationEventArgs)
  115. Me.NavigationService.Navigate(e.Uri)
  116. End Sub
  117. Private Sub OnLogout(ByVal sender As Object, ByVal e As EventArgs)
  118. Me.MainPivot.IsEnabled = False
  119. Me.MainPivot.Opacity = 0.3R
  120. Me.LogOutStackPanel.Opacity = 1
  121. For Each button In Me.GetApplicationBarItemsByText("log out")
  122. button.IsEnabled = False
  123. Next button
  124. Dim pushNotificationClient = App.CloudClientFactory.ResolvePushNotificationClient()
  125. pushNotificationClient.Disconnect(Sub(r)
  126. Me.Dispatcher.BeginInvoke(Sub()
  127. Me.CleanUp(r.Exception)
  128. End Sub)
  129. End Sub)
  130. End Sub
  131. Private Sub CleanUp(ByVal exception As Exception)
  132. If exception IsNot Nothing Then
  133. MessageBox.Show(exception.Message, "Push Unregistration Error", MessageBoxButton.OK)
  134. End If
  135. Me.MainPivot.IsEnabled = True
  136. Me.MainPivot.Opacity = 1
  137. Me.LogOutStackPanel.Opacity = 0
  138. For Each button In GetApplicationBarItemsByText("log out")
  139. button.IsEnabled = True
  140. Next button
  141. ' Clean the current authentication token, flags and view models.
  142. App.CloudClientFactory.CleanAuthenticationToken()
  143. Me.DataContext = Nothing
  144. Me.MainPivot.Items.Clear()
  145. RemoveIsolatedStorageSetting("UserIsRegistered")
  146. SetApplicationState("UserBackPress", False)
  147. ' Navigate to the log in page.
  148. Me.NavigationService.GoBack()
  149. End Sub
  150. Private Sub OnBeginPushConnection(ByVal sender As Object, ByVal e As EventArgs)
  151. Dim items = Me.GetApplicationBarItemsByText("log out")
  152. For Each item In items
  153. item.IsEnabled = False
  154. Next item
  155. End Sub
  156. Private Sub OnEndPushConnection(ByVal sender As Object, ByVal e As EventArgs)
  157. Dim items = Me.GetApplicationBarItemsByText("log out")
  158. For Each item In items
  159. item.IsEnabled = True
  160. Next item
  161. End Sub
  162. End Class
  163. End Namespace