0

In my code, I do the next thing:

        mouse_down = new MouseButtonEventHandler(Ship_Mouse_Down);
        mouse_move = new MouseEventHandler(Ship_Mouse_Move);
        mouse_up = new MouseButtonEventHandler(Ship_Mouse_Up);

        foreach (Image i in PlayerField.Children)
        {
            i.MouseUp += mouse_up;
            i.MouseDown += mouse_down;
            i.MouseMove += mouse_move;
            i.Source = (ImageSource)Resources["sea"];
        }

This adds handlers to the corresponding Images. Then, when a player pushes the button, I do this:

        foreach (Image i in PlayerField.Children)
        {
            i.MouseDown -= mouse_down;
            i.MouseUp -= mouse_up;
            i.MouseMove -= mouse_move;
        }

But it seems that handlers is still up and I don't know how to disable them for good.

2 Answers2

3

Always remove the eventhandlers first and then add, it might be you are adding them multiple times.

foreach (Image i in PlayerField.Children)
{
    i.MouseUp -= mouse_up;
    i.MouseDown -= mouse_down;
    i.MouseMove -= mouse_move;

    i.MouseUp += mouse_up;
    i.MouseDown += mouse_down;
    i.MouseMove += mouse_move;

    i.Source = (ImageSource)Resources["sea"];
}
Janne Matikainen
  • 5,061
  • 15
  • 21
1

Sorry for inconviniance, I found that there is same handlers, but applied to containers of Images in XAML, so that's why I couldn't find them.