加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

wpf – 如何停止每次打开一个新窗口?

发布时间:2020-12-13 22:37:53 所属栏目:Windows 来源:网络整理
导读:我有一个 WPF应用程序,在该应用程序中,单击菜单项会打开一个窗口.如果在窗口打开时再次单击相同的菜单项,则会打开一个新窗口,但我不希望每次都打开一个新窗口. 我需要的是,如果窗口已经打开,则应该关注相同的窗口而不是新窗口. 解决方法 //First we must cre
我有一个 WPF应用程序,在该应用程序中,单击菜单项会打开一个窗口.如果在窗口打开时再次单击相同的菜单项,则会打开一个新窗口,但我不希望每次都打开一个新窗口.

我需要的是,如果窗口已经打开,则应该关注相同的窗口而不是新窗口.

解决方法

//First we must create a object of type the new window we want the open.
NewWindowClass newWindow;

private void OpenNewWindow() {
    //Check if the window wasn't created yet
    if (newWindow == null)
    {
        //Instantiate the object and call the Open() method 
        newWindow= new NewWindowClass();
        newWindow.Show();
        //Add a event handler to set null our window object when it will be closed
        newWindow.Closed += new EventHandler(newWindow_Closed);
    }
    //If the window was created and your window isn't active
    //we call the method Activate to call the specific window to front
    else if (newWindow != null && !newWindow.IsActive)
    {
        newWindow.Activate();
    }
}
void newWindow_Closed(object sender,EventArgs e)
{
    newWindow = null;
}

我认为这可以解决你的问题.

ATT,

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读