Select to view content in your preferred language

Navigation Control & MVVM

787
2
09-01-2011 06:07 AM
BenTreptow1
Deactivated User
I have an application that requires my map to be built in code so that the extent and maximum resolution can be set to a particular user. The extent varies per user and i need to set the maximum resolution for each user to limit the zoom slider on the navigation control. The problem is, even though i explicitly set the max and min resolution for my map in code, the navigation control zoom slider still "minimizes" when the map loads. or atleast appears to be happening at the map.loaded event. this is all being done using mvvm too. anybody have any ideas?

<UserControl x:Class="MVVM_MapDev.MainPage"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:esri="http://schemas.esri.com/arcgis/client/2009"
             xmlns:viewModel="clr-namespace:MVVM_MapDev.ViewModel;assembly=MVVM_MapDev.ViewModel"
             mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400">
    <Grid x:Name="LayoutRoot" Background="Black">
     <Grid.DataContext>
      <viewModel:MainPageViewModel/>
     </Grid.DataContext>
        <ContentPresenter Content="{Binding Path=BaseMap}"/>
        <esri:Navigation Map="{Binding Path=BaseMap}"/>
    </Grid>
</UserControl>


        
void SetBaseMap()
        {
            try
            {
                BaseMap = new Map();
                BaseMap.BorderBrush = new SolidColorBrush(Colors.Gray);
                BaseMap.BorderThickness = new Thickness(1.0);
                BaseMap.Extent = GetMapExtent();
                BaseMap.IsLogoVisible = false;
                BaseMap.Layers = MapLayers;
                BaseMap.MinimumResolution = GetMinResolution();
                BaseMap.ExtentChanged += new EventHandler<ExtentEventArgs>(BaseMap_ExtentChanged);
            }

            catch
            {
                throw;
            }
        }
0 Kudos
2 Replies
dotMorten_esri
Esri Notable Contributor
This is a bit off topic, but the first problem you are going to have is that you have a control in your ViewModel. That's a BIG no-no in MVVM, and can cause a lot of problems for you down the line.
Instead you should replace your ContentPresenter with a Map control, and bind the properties to it (fx. the Layers property, Min/Max resolution etc).
This could also be the reason why the navigation control is acting like it is, because it doesn't have a map to work with at load time, but waits until the data context has been created and set.
0 Kudos
BenTreptow1
Deactivated User
Thanks for the response. I was able to fix the problem I was having by using an attached dependency property. The reason I put the map control in the VM before was because I didn't think I could bind to the extent and resolution properties. I'm not really sure how this works, but I saw that someone else had done it this way and it appears to do what I want it to.
   <Grid x:Name="LayoutRoot" Background="Black">
        <Grid.DataContext>
            <viewModel:MainMapViewModel/>
        </Grid.DataContext>
        <esri:Map x:Name="map_BaseMap" Layers="{Binding Path=MapLayers}" helpers:MapHelper.ZoomGeometry="{Binding Path=MapExtent}"/>
        <esri:Navigation Margin="5" Map="{Binding ElementName=map_BaseMap}"/>
        <esri:MapProgressBar Map="{Binding ElementName=map_BaseMap}" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,0,0,5" MinWidth="150"/>
    </Grid>

    public static class MapHelper
    {
        static readonly DependencyProperty zoomGeometry =
            DependencyProperty.RegisterAttached("ZoomGeometry", typeof(Geometry), typeof(MapHelper), new PropertyMetadata(new PropertyChangedCallback(OnZoomGeometryChanged)));
       
        public static Geometry GetZoomGeometry(DependencyObject d)
        {
            return (Geometry)d.GetValue(zoomGeometry);
        }

        public static void SetZoomGeometry(DependencyObject d, Geometry value)
        {
            d.SetValue(zoomGeometry, value);
        }

        private static void OnZoomGeometryChanged(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 = GetZoomGeometry(map);

            if (map.Extent == null)
            {
                map.Extent = newZoomGeometry.Extent;
                map.MaximumResolution = map.Resolution;
            }

            else
                map.ZoomTo(newZoomGeometry);
        }
    }
0 Kudos