Hi,For my MVVM app, i chose to put the Map object in my ViewModel instead of the View. Therefore in Xaml, i bind the ContentControl to it like this:<ContentControl Content="{Binding Map}"/>I'm not sure that is the best way of doing it.. and i'm interested to know how other people tackle this problem with MVVM...thanks
<UserControl x:Class="MvvmLight1.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:esri="clr-namespace:ESRI.ArcGIS.Client;assembly=ESRI.ArcGIS.Client" DataContext="{Binding Main, Source={StaticResource Locator}}"> <Grid x:Name="LayoutRoot"> <esri:Map x:Name="Map" Layers="{Binding LayerCollection}" /> <Button x:Name="addGraphics" Content="Add Graphics" Height="30" Width="75" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="5,5,0,0" Command="{Binding AddGrahics}" /> </Grid> </UserControl>
public class MainViewModel : ViewModelBase { // Constructor public MainViewModel() { if (IsInDesignMode) { } else { ArcGISTiledMapServiceLayer arcGISTiledMapServiceLayer = new ArcGISTiledMapServiceLayer(); arcGISTiledMapServiceLayer.ID = "BaseMap"; arcGISTiledMapServiceLayer.Url = "http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer"; arcGISTiledMapServiceLayer.InitializationFailed += (s, e) => { }; this.LayerCollection = new LayerCollection(); this.LayerCollection.Add(arcGISTiledMapServiceLayer); } } // Layers collection public const string LayersPropertyName = "LayerCollection"; private ESRI.ArcGIS.Client.LayerCollection _layerCollection = null; public ESRI.ArcGIS.Client.LayerCollection LayerCollection { get { return _layerCollection; } set { _layerCollection = value; RaisePropertyChanged(LayersPropertyName); } } // Relay command and method private RelayCommand _addGrahics; public RelayCommand AddGrahics { get { if (_addGrahics == null) { _addGrahics = new RelayCommand(AddGraphicsRelayMethod); } return _addGrahics; } } private void AddGraphicsRelayMethod() { GraphicsLayer graphicsLayer = new GraphicsLayer(); graphicsLayer.ID = "TestLayer"; Graphic graphic = new Graphic() { Geometry = new MapPoint (-8905559, 4163881), Symbol = new SimpleMarkerSymbol() { Color = new SolidColorBrush(Colors.Red), Size = 10 } }; graphicsLayer.Graphics.Add(graphic); this.LayerCollection.Add(graphicsLayer); } }
Can you give any examples or point me in the right direction for... using "an attached property that takes care of handling binding"
public static class MapExtentHelper { public static readonly DependencyProperty MapZoomGeometryProperty = DependencyProperty.RegisterAttached("MapZoomGeometry", typeof(Geometry), typeof(MapExtentHelper), new PropertyMetadata(new PropertyChangedCallback(OnMapZoomGeometryChanged))); public static Geometry GetMapZoomGeometry(DependencyObject d) { return (Geometry)d.GetValue(MapZoomGeometryProperty); } public static void SetMapZoomGeometry(DependencyObject d, Geometry value) { d.SetValue(MapZoomGeometryProperty, value); } private static void OnMapZoomGeometryChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { Map map = d as Map; if (map == null) throw new ArgumentException("DependencyObject must be of type ESRI.ArcGIS.Client.Map"); Geometry newZoomGeometry = GetMapZoomGeometry(map); if (map.Extent == null) { map.Extent = newZoomGeometry.Extent; } else { map.ZoomTo(newZoomGeometry); } } }
<esri:Map Extensions:MapExtentHelper.MapZoomGeometry="{Binding SomeGeometryOnViewModel}"/>
Přihlášení členové mohou přispívat, sledovat aktualizace a další. Jste tu noví? Zaregistrujte si bezplatný účet.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.