Select to view content in your preferred language

Toolbar Add-In:  Handling the Map ExtentChanged Event

880
1
05-23-2012 04:09 PM
ElisabethvanderLeeuw
Emerging Contributor
I'm trying to add a toolbar to the viewer - to implement zoom previous and zoom next functionality.  (if there is a better way please let me know)  I'm currently trying to convert the esri Silverlight ToolBarWidget example to an add-in.  The problem I'm having is trying to handle the Map.ExtentChanged Event.  I am trying to implement this in the code behind.  When I debug it throws this error:  XamlParseException ...  InnerException: Use the "new" keyword to create an object instance.  InnerException:  Check to determine if the object is null before calling the method.  Any help / suggestions would be great.  Thank you.

public PrevNextToolbar()
        {
            InitializeComponent();
         

            MyDrawObject = new Draw(MapApplication.Current.Map)
            {
                FillSymbol = LayoutRoot.Resources["DefaultFillSymbol"] as ESRI.ArcGIS.Client.Symbols.FillSymbol,
                DrawMode = DrawMode.Rectangle
            };
                    

            MapApplication.Current.Map.ExtentChanged += new EventHandler<ExtentEventArgs>(Map_ExtentChanged);                   

            MyDrawObject.DrawComplete += myDrawObject_DrawComplete;           
          
        }

private void Map_ExtentChanged(object sender, ExtentEventArgs e)
         {
            

             if (e.OldExtent == null)
             {
                 _extentHistory.Add(e.NewExtent.Clone());
                 return;
             }

             if (_newExtent)
             {
                 _currentExtentIndex++;

                 if (_extentHistory.Count - _currentExtentIndex > 0)
                     _extentHistory.RemoveRange(_currentExtentIndex, (_extentHistory.Count - _currentExtentIndex));

                 if (_nextExtentImage.IsHitTestVisible == true)
                 {
                     _nextExtentImage.Opacity = 0.3;
                     _nextExtentImage.IsHitTestVisible = false;
                 }

                 _extentHistory.Add(e.NewExtent.Clone());

                 if (_previousExtentImage.IsHitTestVisible == false)
                 {
                     _previousExtentImage.Opacity = 1;
                     _previousExtentImage.IsHitTestVisible = true;
                 }
             }

             else
             {
                 MapApplication.Current.Map.IsHitTestVisible = true;
                 _newExtent = true;
             }
         }
0 Kudos
1 Reply
ElisabethvanderLeeuw
Emerging Contributor
The solution to my problem was waiting until all my Map Layers were Initialized.  That seemed to do the trick.  However, another issue I had was dealing with graphic layers - with the zoom to previous and zoom to next functionality.  It added the extent from all graphics layers into my ... _history array.  I have a work around but its a bit cludgy for now.

public MyToolbar()

{       
     InitializeComponent();

    if ((MapApplication.Current.Map != null) && (MapApplication.Current.Map.Layers != null))
    { 

        MapApplication.Current.Map.Layers.LayersInitialized += new LayerCollection.LayersInitializedHandler(Layers_LayersInitialized); 
     }

}            


private void Layers_LayersInitialized(object sender, System.EventArgs e)
{
                                

        MapApplication.Current.Map.ExtentChanged += new EventHandler<ExtentEventArgs>(Map_ExtentChanged);

}

Elisabeth
0 Kudos