简体   繁体   English

WPF当另一个窗口调用函数时

[英]WPF Call a function when another window calls it

I'm trying to create a login window in my WPF application, which I'm about to finish: 我正在尝试在WPF应用程序中创建一个登录窗口,该窗口将要完成:

Here's what I'm trying to do: 这是我想做的事情:

I have a main window, which creates the login window. 我有一个主窗口,用于创建登录窗口。

In the login window, when the user logs in, I want to send an event "LoginSuccess". 在登录窗口中,当用户登录时,我要发送事件“ LoginSuccess”。

In the main window, which is listening to the login window' events, a function get called. 在侦听登录窗口事件的主窗口中,调用了一个函数。

In fact, I'm trying to do something like that: 实际上,我正在尝试执行以下操作:

public App()
{
    Window LoginWindow = new ...();
    LoginWindow.LoginSucceed += LoginSucceed;
}

void LoginSucceed(object sender, EventArgs e)
{
    // ....
}

How can I do that? 我怎样才能做到这一点?

You need to create a LoginSucceed event in your LoginWindow.xaml.cs class if you haven't already. 您还需要在LoginWindow.xaml.cs类中创建一个LoginSucceed事件。

Create a class level variable that looks something like this: 创建一个看起来像这样的类级变量:

public event RoutedEventHandler LoginSucceed;

After validating the user, you can trigger this event: 验证用户之后,您可以触发此事件:

LoginSucceed(this, null)

You can then hook up to this event from your main window like you have above. 然后,您可以像上面一样从主窗口挂接到该事件。 Is that what your looking for? 那是你要找的吗?

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

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