关闭窗口而不触发 Window_Closed 事件
这是一个 WPF/C# 应用程序,我遇到的更多是设计问题而不是代码问题。
我有 5 个独立的 *.xaml 文件,它们实现了从 window 派生的类。所以,5个窗口。这是一个向导类型的应用程序,因此每个窗口都有后退/下一步按钮,这些按钮将创建一个新窗口、隐藏自身并显示新窗口。
这不是纯粹基于导航的(所以我不使用页面),因为有时我想跳过一个窗口,或返回到第一个窗口等。
每次打开新窗口时,父窗口都会将自己设置为所有者新窗口并隐藏自身。使用后退按钮时,新窗口会显示所有者并自行关闭。这对于导航来说效果很好。
当有人实际关闭窗口而不是使用导航时,我遇到了问题。如果他们关闭窗口、Alt-F4、点击 X 等,它应该关闭整个应用程序。
我的“返回”按钮还会关闭窗口,这会触发 Window_Closed,从而关闭整个应用程序。
我试图想出一种方法,当用户关闭窗口时关闭整个应用程序,但仅关闭窗口(不是隐藏它,实际上是处理它),并在所有者单击返回时向其显示。我似乎无法在不触发前者的情况下完成后者。
有什么建议吗?我只是没有以正确的方式思考这个问题。
This is a WPF/C# application and I'm having more of a design problem than a code problem.
I have 5 separate *.xaml files implementing a class derived from window. So, 5 windows. This is a wizard-type of application so each window has back/next buttons that will create a new window, hide itself, and show the new window.
This isn't purely navigation based (so I'm not using pages) because sometimes I want to skip a window, or go back to the first window, etc.
Every time a new window is opened the parent window sets itself as the owner of the new window and hides itself. When the back button is used the new window shows the owner and closes itself. This works fine for navigation.
I'm having a problem when someone actually closes the window as opposed to using the navigation. If they close the window, Alt-F4, hit the X, etc it should close the whole app.
My 'go back' button also closes the window, which triggers Window_Closed, closing the whole app.
I'm trying to think of a way to close the whole app when the user closes the window, but close only the window (not hide it, actually dispose of it) and show the owner when they click back. I can't seem to do the latter without triggering the former.
Any suggestions? I'm just not thinking about this the right way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议不要在 Windows 之间切换,而是在面板中的 UserControls 之间切换。您拥有窗口的全部功能,只是您不必担心其打开/关闭状态。只需让用户控件根据下一个/上一个命令切换可见性即可。
您可能会考虑考虑为您的向导进行导航。您可以在 WPF 中执行此操作,如此处演示。
通过导航,您将拥有一个托管页面的窗口。然后,您可以发出命令导航到特定页面,它会自动翻转到该页面,而无需担心控件可见性。
Instead of flipping between Windows, I would recommend flipping between UserControls in panels. You have the full functionality of a Window except you don't have to worry about its open/closed state. Just have the UserControls toggle visibility based on the Next/Previous commands.
You might consider looking into doing navigation for your wizard. You can do this in WPF as demonstrated here.
With navigation, you have a single window that hosts pages. You can then issue a command to navigate to a particular page and it will automatically flip to the page without you having to worry about control visibility.
首先,我不会创建5个不同的窗口,而是创建一个窗口,并在向导页面更改时替换它的内容。这对于用户来说看起来更自然,并且问题根本不会显现出来。
其次,如果您坚持使用多个窗口的设计,我只需在按钮单击处理程序中取消订阅“关闭”(并在重新打开窗口时重新订阅)。
First of all, I would not make 5 different windows, but a single window, and replace the content of it when the wizard page changes. This would look more natural for the user, and the problem won't manifest itself at all.
Second, if you insist on the design with multiple windows, I would just unsubscribe from Close in button Click handler (and resubscribe when I reopen the window).