Mini Kabibi Habibi

Current Path : C:/Users/Public/Documents/DXperience 13.1 Demos/WPF/VB/MapDemo.Wpf/Modules/
Upload File :
Current File : C:/Users/Public/Documents/DXperience 13.1 Demos/WPF/VB/MapDemo.Wpf/Modules/PhotoGallery.xaml.vb

Imports Microsoft.VisualBasic
Imports System
Imports System.Windows
Imports DevExpress.Xpf.Map
Imports System.Windows.Input
Imports System.Xml.Linq
Imports System.Collections.ObjectModel
Imports System.Windows.Media.Imaging
Imports System.Collections.Generic
Imports System.Windows.Data
Imports System.Globalization

Namespace MapDemo
	Partial Public Class PhotoGallery
		Inherits MapDemoModule
		Private ReadOnly Property ViewModel() As CitiesViewModel
			Get
				Return TryCast(LayoutRoot.DataContext, CitiesViewModel)
			End Get
		End Property

		Public Sub New()
			InitializeComponent()
			AddHandler tileLayer.ViewportChanged, AddressOf TileLayer_ViewportChanged
			Dim viewModel As New CitiesViewModel(map, TryCast(Resources("citySmallIconTemplate"), DataTemplate))
			LayoutRoot.DataContext = viewModel
			placePointer.Content = viewModel
		End Sub
		Private Sub TileLayer_ViewportChanged(ByVal sender As Object, ByVal e As ViewportChangedEventArgs)
			navWindow.LeftTop = e.TopLeft
			navWindow.RightBottom = e.BottomRight
		End Sub
		Private Sub GalleryItemClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
			Dim item As PhotoGalleryItemControl = TryCast(sender, PhotoGalleryItemControl)
			If item IsNot Nothing Then
				ViewModel.SelectedPlace = TryCast(item.DataContext, PlaceInfo)
			End If
		End Sub
		Private Sub OnGalleryClose(ByVal sender As Object, ByVal e As RoutedEventArgs)
			ViewModel.SelectedCity = Nothing
		End Sub
		Private Sub OnBackClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
			ViewModel.SelectedCity = Nothing
		End Sub
		Private Sub photoGallery_MouseLeftButtonUp(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
			ViewModel.SelectedCity = Nothing
		End Sub
		Private Sub placeControl_ShowNextSight(ByVal sender As Object, ByVal e As RoutedEventArgs)
			ViewModel.ShowNextSight()
		End Sub
		Private Sub placeControl_ShowPreviousSight(ByVal sender As Object, ByVal e As RoutedEventArgs)
			ViewModel.ShowPrevSight()
		End Sub
	End Class
	Public Enum ViewType
		Map
		Gallery
		Detail
	End Enum

	Public Class CitiesViewModel
		Inherits DependencyObject
		Public Shared ReadOnly CitiesProperty As DependencyProperty = DependencyProperty.Register("Cities", GetType(ObservableCollection(Of MapCustomElement)), GetType(CitiesViewModel), New PropertyMetadata(Nothing))
		Public Shared ReadOnly SelectedCityProperty As DependencyProperty = DependencyProperty.Register("SelectedCity", GetType(CityInfo), GetType(CitiesViewModel), New PropertyMetadata(Nothing, New PropertyChangedCallback(AddressOf SelectedItemPropertyChanged)))
		Public Shared ReadOnly SelectedPlaceProperty As DependencyProperty = DependencyProperty.Register("SelectedPlace", GetType(PlaceInfo), GetType(CitiesViewModel), New PropertyMetadata(Nothing, New PropertyChangedCallback(AddressOf SelectedItemPropertyChanged)))
		Public Shared ReadOnly ViewTypeProperty As DependencyProperty = DependencyProperty.Register("ViewType", GetType(ViewType), GetType(CitiesViewModel), New PropertyMetadata(ViewType.Map, New PropertyChangedCallback(AddressOf ViewTypePropertyChanged)))
		Public Shared ReadOnly CenterPointProperty As DependencyProperty = DependencyProperty.Register("CenterPoint", GetType(GeoPoint), GetType(CitiesViewModel), New PropertyMetadata(New GeoPoint(47.5, 2)))
		Public Shared ReadOnly ZoomLevelProperty As DependencyProperty = DependencyProperty.Register("ZoomLevel", GetType(Integer), GetType(CitiesViewModel), New PropertyMetadata(5))
		Public Shared ReadOnly CityPointProperty As DependencyProperty = DependencyProperty.Register("CityPoint", GetType(Point), GetType(CitiesViewModel), New PropertyMetadata(New Point(0, 0)))
		Public Shared ReadOnly CitySmallIconsProperty As DependencyProperty = DependencyProperty.Register("CitySmallIcons", GetType(ObservableCollection(Of MapCustomElement)), GetType(CitiesViewModel), New PropertyMetadata(Nothing))

		Public ReadOnly Property Cities() As ObservableCollection(Of MapCustomElement)
			Get
				Return CType(GetValue(CitiesProperty), ObservableCollection(Of MapCustomElement))
			End Get
		End Property
		Public ReadOnly Property CitySmallIcons() As ObservableCollection(Of MapCustomElement)
			Get
				Return CType(GetValue(CitySmallIconsProperty), ObservableCollection(Of MapCustomElement))
			End Get
		End Property
		Public Property SelectedCity() As CityInfo
			Get
				Return CType(GetValue(SelectedCityProperty), CityInfo)
			End Get
			Set(ByVal value As CityInfo)
				SetValue(SelectedCityProperty, value)
			End Set
		End Property
		Public Property SelectedPlace() As PlaceInfo
			Get
				Return CType(GetValue(SelectedPlaceProperty), PlaceInfo)
			End Get
			Set(ByVal value As PlaceInfo)
				SetValue(SelectedPlaceProperty, value)
			End Set
		End Property
		Public Property ViewType() As ViewType
			Get
				Return CType(GetValue(ViewTypeProperty), ViewType)
			End Get
			Set(ByVal value As ViewType)
				SetValue(ViewTypeProperty, value)
			End Set
		End Property
		Public Property CenterPoint() As GeoPoint
			Get
				Return CType(GetValue(CenterPointProperty), GeoPoint)
			End Get
			Set(ByVal value As GeoPoint)
				SetValue(CenterPointProperty, value)
			End Set
		End Property
		Public Property CityPoint() As Point
			Get
				Return CType(GetValue(CityPointProperty), Point)
			End Get
			Set(ByVal value As Point)
				SetValue(CityPointProperty, value)
			End Set
		End Property
		Public Property ZoomLevel() As Integer
			Get
				Return CInt(Fix(GetValue(ZoomLevelProperty)))
			End Get
			Set(ByVal value As Integer)
				SetValue(ZoomLevelProperty, value)
			End Set
		End Property

		Private Shared Sub SelectedItemPropertyChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
			Dim model As CitiesViewModel = TryCast(d, CitiesViewModel)
			If model IsNot Nothing Then
				model.UpdateViewType()
			End If
		End Sub
		Private Shared Sub ViewTypePropertyChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
			Dim model As CitiesViewModel = TryCast(d, CitiesViewModel)
			If model IsNot Nothing Then
				model.Update()
			End If
		End Sub

		Private ReadOnly map As MapControl

		Private ReadOnly Property Layer() As LayerBase
			Get
				Return If(map.Layers.Count > 0, map.Layers(0), Nothing)
			End Get
		End Property

		Public Sub New(ByVal map As MapControl, ByVal citySmallIconTemplate As DataTemplate)
			Me.map = map
			Me.SetValue(CitiesProperty, New ObservableCollection(Of MapCustomElement)())
			Me.SetValue(CitySmallIconsProperty, New ObservableCollection(Of MapCustomElement)())
			LoadDataFromXML(citySmallIconTemplate)
		End Sub
		Private Sub LoadDataFromXML(ByVal citySmallIconTemplate As DataTemplate)
			Dim document As XDocument = DataLoader.LoadXmlFromResources("/Data/CitiesData.xml")
			If document IsNot Nothing Then
				For Each element As XElement In document.Element("Cities").Elements()
					Dim cityName As String = element.Element("CityName").Value
					Dim cityLocation As New GeoPoint(Convert.ToDouble(element.Element("Latitude").Value, CultureInfo.InvariantCulture), Convert.ToDouble(element.Element("Longitude").Value, CultureInfo.InvariantCulture))
					Dim cityInfo As New CityInfo(cityName, cityLocation)
					For Each placeElement As XElement In element.Element("Places").Elements()
						Dim placeName As String = placeElement.Element("Name").Value
						Dim placeLocation As New GeoPoint(Convert.ToDouble(placeElement.Element("Latitude").Value, CultureInfo.InvariantCulture), Convert.ToDouble(placeElement.Element("Longitude").Value, CultureInfo.InvariantCulture))
						Dim placeDescription As String = placeElement.Element("Description").Value
						Dim placeImageUri As New Uri(placeElement.Element("ImageUri").Value, UriKind.RelativeOrAbsolute)
						cityInfo.Places.Add(New PlaceInfo(placeName, cityName, placeLocation, placeDescription, New BitmapImage(placeImageUri)))
					Next placeElement
					Dim binding As New Binding("ViewType") With {.Source = Me, .Converter = New ViewTypeToBoolConverter(), .ConverterParameter = ViewType.Map}
					Dim city As New CityInformationControl() With {.CityInfo = cityInfo}
					city.SetBinding(CityInformationControl.VisibleProperty, binding)
					Dim mapItem As New MapCustomElement() With {.Content = city, .Location = cityLocation}
					AddHandler mapItem.MouseLeftButtonUp, AddressOf OnMouseLeftButtonUp
					AddHandler mapItem.MouseLeftButtonDown, AddressOf OnMouseLeftButtonDown
					Cities.Add(mapItem)
					CitySmallIcons.Add(New MapCustomElement() With {.Content = cityInfo, .ContentTemplate = citySmallIconTemplate, .Location = cityInfo.Location})
				Next element
			End If
		End Sub
		Private Sub UpdateViewType()
			If SelectedCity IsNot Nothing Then
				ViewType = If(SelectedPlace IsNot Nothing, ViewType.Detail, ViewType.Gallery)
			Else
				ViewType = ViewType.Map
			End If
		End Sub
		Private Sub Update()
			Select Case ViewType
				Case ViewType.Map
				CaseLabel1:
					ZoomLevel = 5
				Case ViewType.Gallery
					ZoomLevel = 5
					CityPoint = Layer.GeoToScreenPoint(SelectedCity.Location)
				Case ViewType.Detail
					ZoomLevel = 17
					CenterPoint = SelectedPlace.Location
				Case Else
					GoTo CaseLabel1
			End Select
		End Sub
		Private Sub OnMouseLeftButtonUp(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
			Dim element As MapCustomElement = TryCast(sender, MapCustomElement)
			If element IsNot Nothing Then
				SelectedPlace = Nothing
				SelectedCity = (CType(element.Content, CityInformationControl)).CityInfo
				UpdateViewType()
				e.Handled = True
			End If
		End Sub
		Private Sub OnMouseLeftButtonDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
			If TypeOf sender Is MapCustomElement Then
				e.Handled = True
			End If
		End Sub
		Public Sub ShowNextSight()
			If SelectedCity IsNot Nothing AndAlso SelectedPlace IsNot Nothing Then
				Dim index As Integer = SelectedCity.Places.IndexOf(SelectedPlace) + 1
				SelectedPlace = If(index < SelectedCity.Places.Count, SelectedCity.Places(index), SelectedCity.Places(0))
				CenterPoint = SelectedPlace.Location
			End If
		End Sub
		Public Sub ShowPrevSight()
			If SelectedCity IsNot Nothing AndAlso SelectedPlace IsNot Nothing Then
				Dim index As Integer = SelectedCity.Places.IndexOf(SelectedPlace) - 1
				SelectedPlace = If(index < 0, SelectedCity.Places(SelectedCity.Places.Count - 1), SelectedCity.Places(index))
				CenterPoint = SelectedPlace.Location
			End If
		End Sub
	End Class

	Public Class PlaceInfo
		Private ReadOnly name_Renamed As String
		Private ReadOnly cityName_Renamed As String
		Private ReadOnly location_Renamed As GeoPoint
		Private ReadOnly description_Renamed As String
		Private ReadOnly imageSource_Renamed As BitmapImage

		Public ReadOnly Property Name() As String
			Get
				Return name_Renamed
			End Get
		End Property
		Public ReadOnly Property CityName() As String
			Get
				Return cityName_Renamed
			End Get
		End Property
		Public ReadOnly Property Location() As GeoPoint
			Get
				Return location_Renamed
			End Get
		End Property
		Public ReadOnly Property Description() As String
			Get
				Return description_Renamed
			End Get
		End Property
		Public ReadOnly Property ImageSource() As BitmapImage
			Get
				Return imageSource_Renamed
			End Get
		End Property

		Public Sub New(ByVal name As String, ByVal cityName As String, ByVal location As GeoPoint, ByVal description As String, ByVal imageSource As BitmapImage)
			Me.name_Renamed = name
			Me.cityName_Renamed = cityName
			Me.location_Renamed = location
			Me.description_Renamed = description
			Me.imageSource_Renamed = imageSource
		End Sub
	End Class

	Public Class CityInfo
		Private ReadOnly name_Renamed As String
		Private ReadOnly location_Renamed As GeoPoint
		Private ReadOnly places_Renamed As New List(Of PlaceInfo)()

		Public ReadOnly Property Name() As String
			Get
				Return name_Renamed
			End Get
		End Property
		Public ReadOnly Property Location() As GeoPoint
			Get
				Return location_Renamed
			End Get
		End Property
		Public ReadOnly Property Places() As List(Of PlaceInfo)
			Get
				Return places_Renamed
			End Get
		End Property

		Public Sub New(ByVal name As String, ByVal location As GeoPoint)
			Me.name_Renamed = name
			Me.location_Renamed = location
		End Sub
	End Class
End Namespace