2

I can't seem to wire up IActiveViewEvents for an IDockableWindow. I am using a straight out-of-the-box IDockableWindow which inherits BaseCommand. The snippet below shows what I have added to the autogenerated code. Problem is, when I try to run ArcMap, it (the main ArcMap GUI) never loads. Please see attached screenshot to see what I mean.bland arcmap gui

 public override void OnCreate(object hook)
    {
        if (hook != null)
            m_application = hook as IApplication;

        if (m_application != null)
        {
            SetupDockableWindow();
            base.m_enabled = m_dockableWindow != null;
        }
        else
            base.m_enabled = false;

        IMxDocument mxDoc = (IMxDocument)m_application.Document;
        IActiveViewEvents_Event activeViewEvents = mxDoc.ActiveView as IActiveViewEvents_Event;
        activeViewEvents.ViewRefreshed += new IActiveViewEvents_ViewRefreshedEventHandler(activeViewEvents_ViewRefreshed);
    }

    void activeViewEvents_ViewRefreshed(IActiveView view, esriViewDrawPhase phase, object Data, ESRI.ArcGIS.Geometry.IEnvelope envelope)
    {
        MessageBox.Show("ViewRefreshed");
    }
1
  • Is the event being triggered lots and lots of times? You probably should try to hook the event after the OnCreate method, on the first time the Dockable Window is shown. – George Silva Feb 24 '12 at 13:26
4

I'm not sure if this is the source of your problem, but I would suggest not applying the events to the mxDoc.ActiveView within the OnCreate event of the dockable window. The active view can change for a variety of reasons, (user adds a new data frame or opens a new map document). By assigning the events in the OnCreate event you are locking the event handler to the current active view at that point, which means only that view will respond to the event. This creates problems for a number of reasons. For example, if you open ArcMap directly, the currently active view will be the active view in the default mdx. If the user then selects a map document to open, that event handler no longer applies because the active view you applied it to no longer exists.

I typically assign active view events by first creating an ArcMap extension, and then creating an event handler for OnActiveViewChanged. This event is setup either in the Startup method for a blank extension or in the Set method of the State property in a configurable extension. Next I create a private method in the extension for SetupActiveViewEvents and RemoveActiveViewEvents. Whenever the active view is changed I cycle through all of the maps in the current mxDoc and run RemoveActiveViewEvents, and then run SetupActiveViewEvents on the current ActiveView. This does two things, first it insures that my events are visible to all data frames, and second insures the events are not applied multiple times to the same view.

Others may have a better approach, but this has worked very well for me. The example below sets up selection change events on active views. In your case, rather than wiring OnActiveViewSelectionChanged, you could wire the view refresh event.

   '''In the extension, setup the OnActiveViewEvent
    Private Sub SetupDocumentEvents(ByRef mxDoc As IMxDocument)
        m_docEvents = CType(mxDoc, IDocumentEvents_Event)
        AddHandler m_docEvents.ActiveViewChanged, AddressOf OnActiveViewChanged
    End Sub

    ''' ActiveView Changed Event handler
    Private Sub OnActiveViewChanged()
        Dim maps As IMaps2
        Dim map As IMap
        maps = m_mxDoc.Maps

        For i As Integer = 0 To maps.Count - 1
            map = maps.Item(i)
            Me.RemoveActiveViewEvents(map)
        Next

        m_activeView = m_mxDoc.ActiveView
        m_focusMap = m_activeView.FocusMap
        Me.SetupActiveViewEvents(m_focusMap)
    End Sub

    ''' Set up the event handlers for the IActiveViewEvents
    Private Sub SetupActiveViewEvents(ByRef map As ESRI.ArcGIS.Carto.IMap)
        m_activeViewEvents = CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event)
        m_ActiveViewEventsSelectionChanged = New ESRI.ArcGIS.Carto.IActiveViewEvents_SelectionChangedEventHandler(AddressOf OnActiveViewEventsSelectionChanged)
        AddHandler m_activeViewEvents.SelectionChanged, m_ActiveViewEventsSelectionChanged
    End Sub

    ''' Remove the event handlers for the IActiveViewEvents
    Private Sub RemoveActiveViewEvents(ByRef map As ESRI.ArcGIS.Carto.IMap)
        Dim activeViewEvents As ESRI.ArcGIS.Carto.IActiveViewEvents_Event = CType(map, ESRI.ArcGIS.Carto.IActiveViewEvents_Event)
        RemoveHandler activeViewEvents.SelectionChanged, m_ActiveViewEventsSelectionChanged
    End Sub

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.