This project is read-only.

Accessing the Map and Legend controls without using events

Dec 7, 2015 at 8:02 AM
I am building a plugin for MW5 that needs to be able to access and manipulate the displayed map and legend controls, as well as to be able to access the layers that are being displayed in each of them. I have been able to use events like MouseMove, which contains a IMuteMap that I can interact with. However, my plugins has operations that are initiated by pressing one of the buttons in the dockpanel. From there, I need to read what layers are displayed and then do some mathematical and geoprocessing and then add the results to both the database and the map and legend.

I do not see anyway to access these controls except by events, which as I mentioned is something that will cover only a portion of use cases. I am relatively new to MapWindow development, so any assistance or advice would be appreciated.

Dec 7, 2015 at 8:13 AM
Edited Dec 7, 2015 at 8:14 AM

To read layers, I used
IEnumerator layers = context.Map.Layers.GetEnumerator();
while (layers.MoveNext()) { 
  var layer = (ILayer) layers.Current;
  switch (layer.Name) {
do what you want
Dec 7, 2015 at 8:57 AM
Well, it can be done in a bit less verbose way:
foreach (var layer in context.Layers)
    // do whatever you need
Where context is the instance of IAppContext interface which is passed to your plugin in Initialize method. From there you can pass it to your controls, classes or whatever. In MW5 in most cases it's not passed directly but instead injected to constructors (see CompositionRoot class in each plugin, and IAppContext.Container property). But if you don't feel comfortable with the pattern you can do it the old way.

The layers collection is also available via Map.Layers and Legend.Layers. The layer selected in the legend is available via context.Layers.Current property.

Marked as answer by dalguy72 on 12/14/2015 at 11:07 PM
Dec 7, 2015 at 9:29 AM
To make sure I understand, since I may be a bit slower because of the late hour, let me go through the logic, and please correct me any place you believe I may have gone wrong. I have plugin derived from BasePlugin. There is IAppContext variable named _context, and I have it initialized using : public override void Initialize(IAppContext context)
        // Save to local properties:
        _context = context;
        _emdsDockWindow = context.Container.GetSingleton<EMDSDockWindow>();

        _menuGenerator = context.Container.GetInstance<MenuGenerator>();
        _menuListener = context.Container.GetInstance<MenuListener>();
        _mapListener = context.Container.GetInstance<MapListener>();

        // Event handlers are in the MapListener class:

        // adding all the available tools in the toolbox
I have a windows user control that is initialized in the CompositionRoot class in the Compose function, container.RegisterSingleton<myUC>(). There is a context variable named _context. In my usercontrol "myUC " class, I wish to access the _context variable, since it has the appropriate context. When you wrote, I can pass it to any controls or whatever, does that mean I can create a property for the "myUC" class and after initialization, I set this class property, and then I will be able to use it anytime within the program to get the layer (i.e. the context property on my form will always point to the same as the plugin context variable).

Thank you both for your quick replies, I appreciate your answers and support..

Dec 14, 2015 at 3:15 PM
Hi Steve,

Do you still have this question or is it answered by now?

If it is still open can you explain which part is still unclear?