1

In my program I first open the main window. In this window I open some other windows, by clicking a button. While testing my program I discovered two problems. First: If I open a window and close it, it's impossible for me to reopen it. Visual Studio then shows an error message in the codeline Window.Show():

InvalidOperationException was unhandled. Visibility can not set or Show, or ShowDialog WindowInteropHelper.EnsureHandle can not be called after a window has been closed.

The whole code for opening the window, looks like this:

    private void CmdDistance_Click(object senderr, RoutedEventArgs e)
    {
        distance.Show();
    }

It's nearly the same code for opening the other two windows, but I use other buttons. The problem also happens with the other two windows.

The second problem is that, if I first open the Window distance and close it, by clicking the x, and open a other window, the second window indeed opens, but the functions inside the window aren't executed. Currently I use the same code in every window, just for testing but it just works in the first opened window. Still here I don't know what I did wrong. I post here the code I use to calle the function inside the individual windows.

    private void MousePosition(object sender, System.Windows.Input.MouseEventArgs e)
    {
        if (distance!=null && distance.Visibility==Visibility.Visible )
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                myMousePosition = e.GetPosition(DepthImage);
                depthWidth = mySensor.DepthStream.FrameWidth;
                distance.setDistancePosition(myMousePosition, depthWidth, mySkeletonArray);
            }
        }

        else if (rectangle_area != null && rectangle_area.Visibility == Visibility.Visible)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                myMousePosition = e.GetPosition(DepthImage);
                depthWidth = mySensor.DepthStream.FrameWidth;
                rectangle_area.setRectanglePosition(myMousePosition, depthWidth, mySkeletonArray);
            }
        }
        else if (circle_area != null && circle_area.Visibility == Visibility.Visible)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                myMousePosition = e.GetPosition(DepthImage);
                depthWidth = mySensor.DepthStream.FrameWidth;
                circle_area.setCirclePosition(myMousePosition, depthWidth, mySkeletonArray);
            }
        }
    }

I don't know if the problems work together so I thought I just post both in the same question and hope someone sees what I did wrong.

user3794592
  • 183
  • 2
  • 16

1 Answers1

2

Concerning the first question: Have you tried hiding the window instead of closing it? Have a look at How to re-open the closed window?, maybe it helps.

Community
  • 1
  • 1
eol
  • 23,236
  • 5
  • 46
  • 64
  • 1
    Hey thanks for the link. I now just added a other function for closing and both problems were solved at the same time. Now my windows are just hidden and now I can open, close and reopen one window, and i can open and close one window and I can open a second window after without problems. Thanks you very much. – user3794592 Aug 02 '14 at 18:44