Mini Kabibi Habibi
using System;
using System.Windows;
using DevExpress.Xpf.Map;
using System.Windows.Input;
using System.Xml.Linq;
using System.Collections.ObjectModel;
using System.Windows.Media.Imaging;
using System.Collections.Generic;
using System.Windows.Data;
using System.Globalization;
namespace MapDemo {
public partial class PhotoGallery : MapDemoModule {
CitiesViewModel ViewModel { get { return LayoutRoot.DataContext as CitiesViewModel; } }
public PhotoGallery() {
InitializeComponent();
tileLayer.ViewportChanged += new DevExpress.Xpf.Map.ViewportChangedEventHandler(TileLayer_ViewportChanged);
CitiesViewModel viewModel = new CitiesViewModel(map, Resources["citySmallIconTemplate"] as DataTemplate);
LayoutRoot.DataContext = viewModel;
placePointer.Content = viewModel;
}
void TileLayer_ViewportChanged(object sender, ViewportChangedEventArgs e) {
navWindow.LeftTop = e.TopLeft;
navWindow.RightBottom = e.BottomRight;
}
void GalleryItemClick(object sender, RoutedEventArgs e) {
PhotoGalleryItemControl item = sender as PhotoGalleryItemControl;
if(item != null)
ViewModel.SelectedPlace = item.DataContext as PlaceInfo;
}
void OnGalleryClose(object sender, RoutedEventArgs e) {
ViewModel.SelectedCity = null;
}
void OnBackClick(object sender, RoutedEventArgs e) {
ViewModel.SelectedCity = null;
}
void photoGallery_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) {
ViewModel.SelectedCity = null;
}
void placeControl_ShowNextSight(object sender, RoutedEventArgs e) {
ViewModel.ShowNextSight();
}
void placeControl_ShowPreviousSight(object sender, RoutedEventArgs e) {
ViewModel.ShowPrevSight();
}
}
public enum ViewType {
Map,
Gallery,
Detail
}
public class CitiesViewModel : DependencyObject {
public static readonly DependencyProperty CitiesProperty = DependencyProperty.Register("Cities",
typeof(ObservableCollection<MapCustomElement>), typeof(CitiesViewModel), new PropertyMetadata(null));
public static readonly DependencyProperty SelectedCityProperty = DependencyProperty.Register("SelectedCity",
typeof(CityInfo), typeof(CitiesViewModel), new PropertyMetadata(null, new PropertyChangedCallback(SelectedItemPropertyChanged)));
public static readonly DependencyProperty SelectedPlaceProperty = DependencyProperty.Register("SelectedPlace",
typeof(PlaceInfo), typeof(CitiesViewModel), new PropertyMetadata(null, new PropertyChangedCallback(SelectedItemPropertyChanged)));
public static readonly DependencyProperty ViewTypeProperty = DependencyProperty.Register("ViewType",
typeof(ViewType), typeof(CitiesViewModel), new PropertyMetadata(ViewType.Map, new PropertyChangedCallback(ViewTypePropertyChanged)));
public static readonly DependencyProperty CenterPointProperty = DependencyProperty.Register("CenterPoint",
typeof(GeoPoint), typeof(CitiesViewModel), new PropertyMetadata(new GeoPoint(47.5, 2)));
public static readonly DependencyProperty ZoomLevelProperty = DependencyProperty.Register("ZoomLevel",
typeof(int), typeof(CitiesViewModel), new PropertyMetadata(5));
public static readonly DependencyProperty CityPointProperty = DependencyProperty.Register("CityPoint",
typeof(Point), typeof(CitiesViewModel), new PropertyMetadata(new Point(0, 0)));
public static readonly DependencyProperty CitySmallIconsProperty = DependencyProperty.Register("CitySmallIcons",
typeof(ObservableCollection<MapCustomElement>), typeof(CitiesViewModel), new PropertyMetadata(null));
public ObservableCollection<MapCustomElement> Cities {
get { return (ObservableCollection<MapCustomElement>)GetValue(CitiesProperty); }
}
public ObservableCollection<MapCustomElement> CitySmallIcons {
get { return (ObservableCollection<MapCustomElement>)GetValue(CitySmallIconsProperty); }
}
public CityInfo SelectedCity {
get { return (CityInfo)GetValue(SelectedCityProperty); }
set { SetValue(SelectedCityProperty, value); }
}
public PlaceInfo SelectedPlace {
get { return (PlaceInfo)GetValue(SelectedPlaceProperty); }
set { SetValue(SelectedPlaceProperty, value); }
}
public ViewType ViewType {
get { return (ViewType)GetValue(ViewTypeProperty); }
set { SetValue(ViewTypeProperty, value); }
}
public GeoPoint CenterPoint {
get { return (GeoPoint)GetValue(CenterPointProperty); }
set { SetValue(CenterPointProperty, value); }
}
public Point CityPoint {
get { return (Point)GetValue(CityPointProperty); }
set { SetValue(CityPointProperty, value); }
}
public int ZoomLevel {
get { return (int)GetValue(ZoomLevelProperty); }
set { SetValue(ZoomLevelProperty, value); }
}
static void SelectedItemPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
CitiesViewModel model = d as CitiesViewModel;
if(model != null)
model.UpdateViewType();
}
static void ViewTypePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
CitiesViewModel model = d as CitiesViewModel;
if(model != null)
model.Update();
}
readonly MapControl map;
LayerBase Layer { get { return map.Layers.Count > 0 ? map.Layers[0] : null; } }
public CitiesViewModel(MapControl map, DataTemplate citySmallIconTemplate) {
this.map = map;
this.SetValue(CitiesProperty, new ObservableCollection<MapCustomElement>());
this.SetValue(CitySmallIconsProperty, new ObservableCollection<MapCustomElement>());
LoadDataFromXML(citySmallIconTemplate);
}
void LoadDataFromXML(DataTemplate citySmallIconTemplate) {
XDocument document = DataLoader.LoadXmlFromResources("/Data/CitiesData.xml");
if(document != null) {
foreach(XElement element in document.Element("Cities").Elements()) {
string cityName = element.Element("CityName").Value;
GeoPoint cityLocation = new GeoPoint(Convert.ToDouble(element.Element("Latitude").Value, CultureInfo.InvariantCulture),
Convert.ToDouble(element.Element("Longitude").Value, CultureInfo.InvariantCulture));
CityInfo cityInfo = new CityInfo(cityName, cityLocation);
foreach(XElement placeElement in element.Element("Places").Elements()) {
string placeName = placeElement.Element("Name").Value;
GeoPoint placeLocation = new GeoPoint(Convert.ToDouble(placeElement.Element("Latitude").Value, CultureInfo.InvariantCulture),
Convert.ToDouble(placeElement.Element("Longitude").Value, CultureInfo.InvariantCulture));
string placeDescription = placeElement.Element("Description").Value;
Uri placeImageUri = new Uri(placeElement.Element("ImageUri").Value, UriKind.RelativeOrAbsolute);
cityInfo.Places.Add(new PlaceInfo(placeName, cityName, placeLocation, placeDescription, new BitmapImage(placeImageUri)));
}
Binding binding = new Binding("ViewType") { Source = this, Converter = new ViewTypeToBoolConverter(), ConverterParameter = ViewType.Map };
CityInformationControl city = new CityInformationControl() { CityInfo = cityInfo };
city.SetBinding(CityInformationControl.VisibleProperty, binding);
MapCustomElement mapItem = new MapCustomElement() { Content = city, Location = cityLocation };
mapItem.MouseLeftButtonUp += new System.Windows.Input.MouseButtonEventHandler(OnMouseLeftButtonUp);
mapItem.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(OnMouseLeftButtonDown);
Cities.Add(mapItem);
CitySmallIcons.Add(new MapCustomElement() { Content = cityInfo, ContentTemplate = citySmallIconTemplate, Location = cityInfo.Location });
}
}
}
void UpdateViewType() {
if(SelectedCity != null)
ViewType = SelectedPlace != null ? ViewType.Detail : ViewType.Gallery;
else
ViewType = ViewType.Map;
}
void Update() {
switch(ViewType) {
case ViewType.Map:
ZoomLevel = 5;
break;
case ViewType.Gallery:
ZoomLevel = 5;
CityPoint = Layer.GeoToScreenPoint(SelectedCity.Location);
break;
case ViewType.Detail:
ZoomLevel = 17;
CenterPoint = SelectedPlace.Location;
break;
default:
goto case ViewType.Map;
}
}
void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e) {
MapCustomElement element = sender as MapCustomElement;
if(element != null) {
SelectedPlace = null;
SelectedCity = ((CityInformationControl)element.Content).CityInfo;
UpdateViewType();
e.Handled = true;
}
}
void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
if(sender is MapCustomElement)
e.Handled = true;
}
public void ShowNextSight() {
if (SelectedCity != null && SelectedPlace != null) {
int index = SelectedCity.Places.IndexOf(SelectedPlace) + 1;
SelectedPlace = index < SelectedCity.Places.Count ? SelectedCity.Places[index] : SelectedCity.Places[0];
CenterPoint = SelectedPlace.Location;
}
}
public void ShowPrevSight() {
if (SelectedCity != null && SelectedPlace != null) {
int index = SelectedCity.Places.IndexOf(SelectedPlace) - 1;
SelectedPlace = index < 0 ? SelectedCity.Places[SelectedCity.Places.Count - 1] : SelectedCity.Places[index];
CenterPoint = SelectedPlace.Location;
}
}
}
public class PlaceInfo {
readonly string name;
readonly string cityName;
readonly GeoPoint location;
readonly string description;
readonly BitmapImage imageSource;
public string Name { get { return name; } }
public string CityName { get { return cityName; } }
public GeoPoint Location { get { return location; } }
public string Description { get { return description; } }
public BitmapImage ImageSource { get { return imageSource; } }
public PlaceInfo(string name, string cityName, GeoPoint location, string description, BitmapImage imageSource) {
this.name = name;
this.cityName = cityName;
this.location = location;
this.description = description;
this.imageSource = imageSource;
}
}
public class CityInfo {
readonly string name;
readonly GeoPoint location;
readonly List<PlaceInfo> places = new List<PlaceInfo>();
public string Name { get { return name; } }
public GeoPoint Location { get { return location; } }
public List<PlaceInfo> Places { get { return places; } }
public CityInfo(string name, GeoPoint location) {
this.name = name;
this.location = location;
}
}
}