Select to view content in your preferred language

MapApplication.Current.ShowWindow Close event handlers in Add-Ins.

2707
1
07-22-2013 12:09 PM
januszkrawczynski
Deactivated User
Hello,
On execution of this code as an Add-In Tool in Silverlight Viewer, when the dialog menu is closed the message informs the user.
Why the event method is run multiple times on the closing. If it is run once the dialog pops up once. If run second time (click on the tool in the tool bar menu) the dialog box pops up twice. If tool is clicked 5 times the dialog menu will close once and the event handler (message) is displayed 5 times?
How can we explain that behavior?
What to do to make the event handler run once on every dialog box closing?

namespace ViewerGetParcel.AddIns
{
    [Export(typeof(ICommand))]
    [DisplayName("Identify Parcel")]
    public class MyTool : ICommand
    {  
        // Event Declaration
        public event EventHandler DialogHide;

        public void Execute(object parameter)
        {  
            DialogHide += new EventHandler(stopDialog);

            MapApplication.Current.ShowWindow("My Tool", new TextBox(), false, null, DialogHide);
        }

        public bool CanExecute(object parameter)
        {
            // Return true so that the command can always be executed
            return true;
        }

        public event EventHandler CanExecuteChanged;

        public void stopDialog(Object sender, EventArgs ea)
        {
            MessageBox.Show("Dialog Closed");
        }
    }
}


Cheers,
Janusz
0 Kudos
1 Reply
januszkrawczynski
Deactivated User
Because you are creating the invocation list of event handlers each time you execute the tool.
When you fire the event all the events in the list are invoked.
Answer:
UnRegister the event with the method by this:

DialogHide -= new EventHandler(stopDialog);

Thanks,
Janusz
0 Kudos