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
This is how I am implementing in my MVVM app (using MVVM lite frame work)Let me know if there is a better wayXAML
<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>
ViewModel
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);
}
}