从另一个窗口访问主窗口

发布于 2024-11-04 18:17:37 字数 138 浏览 4 评论 0原文

我创建了一个从主窗口打开的新窗口。单击此窗口中的按钮后,窗口应关闭并同时触发主窗​​口中的某些事件。

我认为这很容易实现,但我不知道如何访问 MainWindow 方法和来自另一个窗口的字段...

任何帮助将不胜感激。谢谢! :)

I have created a new Window that opens up from the MainWindow. Upon clicking a button from this Window, the Window should close and at the same time trigger some event in MainWindow.

I thought this would be easy to implement but I don't know how to access MainWindow methods & fields from another Window...

Any help would be appreciated. Thanks! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

剩余の解释 2024-11-11 18:18:10

这很简单,因为您希望在子窗口关闭时触发 MainWindow 中的操作,只需注册 close 事件:

// In the main window, whenever your start your child window (not in the constructor)
private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        var window = new Window();
        window.Closed += new EventHandler(window_Closed);
        window.ShowDialog();
    }

    void window_Closed(object sender, EventArgs e)
    {
        throw new NotImplementedException();
    }

然后当子窗口关闭时将调用主窗口中的函数 window_Closed 。

This is quite simple since you want an action in the MainWindow to be triggered when the child window closes, simply register the close event :

// In the main window, whenever your start your child window (not in the constructor)
private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        var window = new Window();
        window.Closed += new EventHandler(window_Closed);
        window.ShowDialog();
    }

    void window_Closed(object sender, EventArgs e)
    {
        throw new NotImplementedException();
    }

Then the function window_Closed in the main window will be called when the child window is closed.

红衣飘飘貌似仙 2024-11-11 18:18:09

您不需要以这种方式执行此操作...

在子窗口中定义一个事件,例如:

public event EventHandler Closing;

当您在主窗口中创建此窗口时订阅它:

Window1 wnd = new Window1();

wnd.Closing += new EventHandler(childClosing);

当关闭子窗口时检查事件是否被订阅并引发它

if (Closing != null) {
  Closing(this, new EventArgs());
}

:当然,您需要在主窗口中使用 childClosing 方法:

private void childClosing(object sender, EvenrArgs e) {
  /// do your work
}

如果您需要传回一些数据,那么您可以创建一个派生自 EvenrArgs 的类并使用它。但是您需要一个与 Eventhandler 不同的委托,例如:

class ClosingArgs : EventArgs {
   // some property here
}

在子窗口中

delegate void ClosingEventHandler(object sender, ClosingArgs e);

public event ClosingEventHandler Closing;

欢呼!

You dont need to do that in this way...

define in the child window an event like:

public event EventHandler Closing;

when you create this window in the main window subscribe to it:

Window1 wnd = new Window1();

wnd.Closing += new EventHandler(childClosing);

when closing the child window check if the event is subscribed and raise it:

if (Closing != null) {
  Closing(this, new EventArgs());
}

of course you need a childClosing method in the main window:

private void childClosing(object sender, EvenrArgs e) {
  /// do your work
}

If you need to pass some data back then you create a class that derives from EvenrArgs and use that instead. But you need a delegate different from Eventhandler like:

class ClosingArgs : EventArgs {
   // some property here
}

in the child window

delegate void ClosingEventHandler(object sender, ClosingArgs e);

and

public event ClosingEventHandler Closing;

cheers!

风蛊 2024-11-11 18:18:08

这是错误的做法...子窗口应该有一个 Closed 事件,您可以为其注册处理程序。如果您确实需要在子窗口关闭之前从子窗口执行父窗口函数,那么您可以在打开子窗口时将委托传递给子窗口。

That is the wrong way to do it... the child window should have a Closed event that you can register a handler for. If you really need to execute parent window functions from the child before it has closed then you can pass delegates in to the child when you open it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文