AnsweredAssumed Answered

Custom extension not working when enabled on startup VB.Net

Question asked by purplecruz on Aug 25, 2016

I have created an extension Add In that should start listening to editor events when it is checked on.  Everything is working great if the extension is not checked on startup.  I check the extension on and everything works as expected.  The problem is when I leave the extension checked on.  When I reopen the map, I see my message boxes on startup, but the editor listeners do not work.  This is an autoload extension not a Just In Time (JIT) so autoload is set to true in my config file.

 

I have searched every example and help topic I can find and am stumped.  Any help would be appreciated!

Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.Editor
Imports ESRI.ArcGIS.Desktop.AddIns
Imports ESRI.ArcGIS.Geodatabase
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.Framework

Public Class Default_GDRC_Num_ext
    Inherits ESRI.ArcGIS.Desktop.AddIns.Extension

    Public Sub New()
    End Sub

    Private Shared s_Extension As Default_Num_ext

    Protected Overrides Sub OnStartup()
        s_Extension = Me
        Initialize()
    End Sub

    Protected Overrides Function OnSetState(ByVal state As ESRI.ArcGIS.Desktop.AddIns.ExtensionState) As Boolean
        Me.State = state
        If state = ESRI.ArcGIS.Desktop.AddIns.ExtensionState.Enabled Then
            MsgBox("Enabled text")
            Initialize()
            AddHandler Events.OnStartEditing, AddressOf Events_OnStartEditing
            AddHandler Events.OnStopEditing, AddressOf Events_OnStopEditing
        Else
            MsgBox("Not Enabled text")
            Uninitialize()
        End If
        Return MyBase.OnSetState(state)
    End Function

    Protected Overrides Function OnGetState() As ESRI.ArcGIS.Desktop.AddIns.ExtensionState
        Return Me.State
    End Function

    Private Sub Initialize()
        If Me.State <> ExtensionState.Enabled Then
            Return
        End If
        MsgBox("Initializing")
        AddHandler Events.OnStartEditing, AddressOf Events_OnStartEditing
        AddHandler Events.OnStopEditing, AddressOf Events_OnStopEditing
    End Sub

    Private Sub Uninitialize()
        If s_Extension Is Nothing Then
            Return
        End If
        MsgBox("Uninitializing")
        RemoveHandler Events.OnStartEditing, AddressOf Events_OnStartEditing
    End Sub

    Private ReadOnly Property MyEditor() As IEditor
        Get
            Dim m_editor As IEditor
            Dim editorUid As New UID()
            editorUid.Value = "esriEditor.Editor"
            m_editor = TryCast(My.ArcMap.Application.FindExtensionByCLSID(editorUid), IEditor)
            Return TryCast(m_editor, IEditor)
        End Get
    End Property

    Private ReadOnly Property Events() As IEditEvents_Event
        Get
            Dim m_editorEvents As IEditEvents_Event = TryCast(MyEditor, IEditEvents_Event)
            Return TryCast(m_editorEvents, IEditEvents_Event)
        End Get
    End Property

    Private Sub Events_OnStartEditing()
        MsgBox("you are now editing")
    End Sub

    Private Sub Events_OnStopEditing()
        MsgBox("You will stop editing")
    End Sub

    Protected Overrides Sub OnShutdown()
        s_Extension = Nothing
        Uninitialize()
        RemoveHandler Events.OnStartEditing, AddressOf Events_OnStartEditing
    End Sub

Outcomes