从另一个窗口访问主窗口
我创建了一个从主窗口打开的新窗口。单击此窗口中的按钮后,窗口应关闭并同时触发主窗口中的某些事件。
我认为这很容易实现,但我不知道如何访问 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这很简单,因为您希望在子窗口关闭时触发 MainWindow 中的操作,只需注册 close 事件:
然后当子窗口关闭时将调用主窗口中的函数 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 :
Then the function window_Closed in the main window will be called when the child window is closed.
您不需要以这种方式执行此操作...
在子窗口中定义一个事件,例如:
当您在主窗口中创建此窗口时订阅它:
当关闭子窗口时检查事件是否被订阅并引发它
:当然,您需要在主窗口中使用 childClosing 方法:
如果您需要传回一些数据,那么您可以创建一个派生自 EvenrArgs 的类并使用它。但是您需要一个与 Eventhandler 不同的委托,例如:
在子窗口中
,
欢呼!
You dont need to do that in this way...
define in the child window an event like:
when you create this window in the main window subscribe to it:
when closing the child window check if the event is subscribed and raise it:
of course you need a childClosing method in the main window:
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:
in the child window
and
cheers!
这是错误的做法...子窗口应该有一个
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.使用 Application.MainWindow 属性
Use Application.MainWindow property