简体   繁体   English

如何通过按钮调用另一个窗口?

[英]How Do I Call Another Window With Button Press?

I want to be able to press a button and have the program open up a new window and close the old one. 我希望能够按一个按钮,使程序打开一个新窗口,然后关闭旧窗口。

I have followed solutions from this link but i have never has success with any of them How do I open a second window from the first window in WPF? 我已经从此链接中获得了解决方案,但是我从未成功过, 如何在WPF中从第一个窗口打开第二个窗口?

Here is my work soo far: 这是我到目前为止的工作:

Window editor = new Window();
editor.Show();
this.Close();

But this does nothing. 但这无济于事。

The program should open up a new window and close the old one. 该程序应打开一个新窗口,然后关闭旧窗口。

The functionality you described will work just fine. 您描述的功能可以正常工作。 The Problem there is would more likely be the function or Methode in which you call this function. 问题所在很可能是您在其中调用此函数的函数或Methode。

To write a Methode that would handle a Button press as you want is pretty good described here: https://www.c-sharpcorner.com/forums/c-sharp-button-click-hold-and-release . 要编写一个可以根据需要处理Button按下的Methode,这里描述得相当不错: https : //www.c-sharpcorner.com/forums/c-sharp-button-click-hold-and-release

Hopefully, this will help you otherwise just ask 希望这会对您有所帮助

here is a small Implementation if that helps: 如果有帮助,这里有一个小的实现:

public partial class MainWindow : Window
{
    private void MainWindow_KeyDown(object sender, KeyEventArgs e)
    {
        Window editor = new MainWindow();
        editor.Show();
        this.Close();
    }

    private void MainWindow_KeyUP(object sender, KeyEventArgs e)
    {

    }
    public MainWindow()
    {
        this.KeyDown += MainWindow_KeyDown;
        this.KeyUp += MainWindow_KeyUP;
    }
}

You have to call the second window from the first. 您必须从第一个窗口调用第二个窗口。 This is something I did for a project where it popped up a new login panel window: 这是我为一个项目执行的操作,它弹出了一个新的登录面板窗口:

 private void displayLoginPanel_Click(object sender, EventArgs e) 
 {
     LoginPanel myLogin = new LoginPanel(this);
     myLogin.Show();
     this.Hide();
 }

I used hide() instead of close() because you can see that I am sending a reference of the parent to the child LoginPanel in order to come back later. 我使用hide()而不是close(),因为您可以看到我将父级的引用发送给子级LoginPanel,以便稍后返回。 You can replace the Hide() with Close(). 您可以将Close()替换为Hide()。

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

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