【问题标题】:Closing and reopening Window [duplicate]关闭和重新打开窗口[重复]
【发布时间】:2014-09-25 17:07:51
【问题描述】:

在我的程序中,我首先打开主窗口。在这个窗口中,我通过单击一个按钮打开了一些其他窗口。在测试我的程序时,我发现了两个问题。 第一:如果我打开一个窗口然后关闭它,我就不可能重新打开它。 Visual Studio 然后在代码行 Window.Show() 中显示一条错误消息:

InvalidOperationException 未处理。无法设置或显示可见性,或在窗口关闭后无法调用 ShowDialog WindowInteropHelper.EnsureHandle。

打开窗口的整个代码如下:

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

打开其他两个窗口的代码几乎相同,但我使用其他按钮。其他两个窗口也会出现此问题。

第二个问题是,如果我先打开窗口距离并关闭它,通过单击 x,然后打开另一个窗口,第二个窗口确实打开了,但窗口内的功能没有执行。目前我在每个窗口中都使用相同的代码,只是为了测试,但它只适用于第一个打开的窗口。还在这里,我不知道我做错了什么。 我在这里发布了我用来在各个窗口中调用函数的代码。

    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);
            }
        }
    }

我不知道这些问题是否一起解决,所以我想我只是在同一个问题中发布了两个问题,并希望有人看到我做错了什么。

【问题讨论】:

    标签: c# wpf function window


    【解决方案1】:

    关于第一个问题:您是否尝试过隐藏窗口而不是关闭它?看看How to re-open the closed window?,也许有帮助。

    【讨论】:

    • 您好,感谢您的链接。我现在只是添加了另一个关闭功能,并且同时解决了两个问题。现在我的窗口只是隐藏了,现在我可以打开、关闭和重新打开一个窗口,我可以打开和关闭一个窗口,然后我可以毫无问题地打开第二个窗口。非常感谢。
    猜你喜欢
    • 2020-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多