简体   繁体   English

在WPF中从一个窗口切换到另一个窗口并使其处于活动状态

[英]Switching from one window to another in WPF and making it active

I'm pretty new to WPF and I'm trying to make a database system. 我是WPF的新手,我正在尝试建立一个数据库系统。 What I currently have is a Login Window. 我目前拥有的是一个Login窗口。 When you enter the user and password you are supposed to go to another window StudentInfoSystem . 当您输入用户名和密码时,您应该转到另一个窗口StudentInfoSystem The code I used is pretty basic and common. 我使用的代码非常基本和常见。

var info = new StudentInfoSystem.MainWindow();
info.Show();        
this.Close();

So, what this would do, is after you press the login button, you get transferred to StudentInfoSystem and the login window closes. 那么,这样做,就是在你按下登录按钮后,你将被转移到StudentInfoSystem并关闭登录窗口。 The problem I have with Show() is that it opens a window and immediately returns, right? 我对Show()是它打开一个窗口并立即返回,对吧? It doesn't wait for the new window to close. 它不会等待新窗口关闭。 So, my question is how can I open a new window and WORK with it? 所以,我的问题是如何打开一个新窗口并使用它? When I say work, I meant to show out information in my textboxes (In the NEWLY opened window) depending on the role the user has, etc... 当我说工作时,我的意思是在我的文本框中显示信息(在新打开的窗口中),具体取决于用户的角色等...

I'm guessing the above code is in the button click handler for the Login Window, which would make the Login Window the parent of the StudentInfoSystem window. 我猜测上面的代码位于登录窗口的按钮单击处理程序中,这将使登录窗口成为StudentInfoSystem窗口的父级。
Since WPF will close the parent and any child(ren) window(s) when closing the parent window, your StudentInfo window will also close when calling 由于WPF将在关闭父窗口时关闭父窗口和任何子窗口,因此您的StudentInfo窗口也将在关闭时关闭

this.Close();

One option might be to instead call 一种选择可能是打电话

this.Hide();

but without seeing how the rest of your app is set up, not 100% sure this is the best approach. 但没有看到你的应用程序的其余部分是如何设置的,不是100%确定这是最好的方法。

Perhaps see these SO questions: 也许看到这些SO问题:

  1. wpf-create-sibling-window-and-close-current-one WPF创建同胞窗口和关闭电流,一个

  2. how-to-close-current-window-in-code-when-launching-new-window 如何对关闭电流窗口功能于代码时,启动新窗口

尝试使用window.Activate()来聚焦新窗口和/或[any element].Focus()以聚焦窗口中的任何元素。

As I understand, this should do what you want: 据我了解,这应该做你想要的:

info.ShowDialog();

You could also check ShutdownMode property. 您还可以检查ShutdownMode属性。 I would rather say, login window is sth you want to close after login, but do what you want :). 我宁愿说,登录窗口是你想要在登录后关闭,但做你想要的:)。 Usage of ShutdownMode property: ShutdownMode属性的用法:

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);    
        this.ShutdownMode = System.Windows.ShutdownMode.OnLastWindowClose;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM