检测应用程序退出以及在未保存更改时如何停止

发布于 2024-08-23 14:16:40 字数 371 浏览 5 评论 0原文

使用 Microsoft 的 Composite Application Guidance 工具,似乎没有机制允许您查询 modules ViewModel 并询问其中是否有未保存的数据。这是一个问题,因为我也对此感到内疚,当有未保存的数据时,您无法阻止应用程序终止...

我曾考虑过创建一个名为 IApplicationEvents 的接口,并在那里有一个名为 ApplicationExiting 的事件。想法是每个模块都可以订阅该事件,并且在触发时可以发回“Cancel=true”或“Cancel=false”来表示是否允许应用程序退出。

很想知道其他人在这种情况下可能做了什么,并想看看社区中有哪些可能的解决方案来解决这个问题。

谢谢。

Using the Composite Application Guidance tools from Microsoft, It seems as if there is no mechanism to allow you to query your modules ViewModels and ask if any of them have unsaved data. This is a problem because, and I'm guilty of this as well, you cannot stop the application from terminating when there is unsaved data...

I had thought about creating an interface called IApplicationEvents and have an event on there called ApplicationExiting. The thought being that each module can subscribe to the event and, when fired, can send back a "Cancel=true" or "Cancel=false" to say whether or not to allow the application exiting.

Curious to find out what others may have done in this instance, and to see what possible solutions there are in the community to solve this issue.

Thx.

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

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

发布评论

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

评论(1

我最亲爱的 2024-08-30 14:16:40

这里有很多选择。

首先,我想澄清一些术语......通常,模块程序集中包含的视图或视图模型是未保存更改的内容,而不是模块本身。模块负责在开始时实例化任何必要的视图,并在初始化期间向 shell 做出贡献,通常就是这样,因此当您解决此问题时,您需要关注视图/视图模型而不是模块类。

我脑海中浮现的选项是:

  1. 采用像 Caliburn 这样的免费框架,它支持这样的应用程序事件(以及一些 MDI 事件,例如 ViewClosing 等)。它内置了对 Prism 的支持 (http://caliburn.codeplex.com/)

  2. 使用复合命令。您的视图或视图模型将在其他地方使用复合命令(CloseCommand,您为应用程序静态声明)注册自己,并且每个打开的视图将触发其 CanExecute 和 Execute 方法,以便您既可以在关闭应用程序时投票,也可以做出反应无论如何,如果它发生的话。 CompositeCommands 是 Prism 的一项功能。 (请参阅:命令快速入门

我认为这些可能是最优雅的。还有更多选择,但这些选择与现有惯例保持着最佳的和谐。

希望这有帮助。

There are a lot of choices here.

First off, I wanted to clarify a little nomenclature... typically your Views or ViewModels contained within your Module assemblies are the things with unsaved changes, not the Module itself. The Module is responsible for instantiating any views necessary at the start and contributing back to the shell during Initialize and that's typically it, so when you attack this problem, you'll want to focus on your views/viewmodels and not the Module classes.

Options off the top of my head:

  1. Adopt a complimentary framework like Caliburn that has support for application events like this (as well as some MDI events like ViewClosing, that kind of thing). It has builtin support for Prism (http://caliburn.codeplex.com/)

  2. Use a composite command. Your views or viewmodels will register themselves with a composite command elsewhere (CloseCommand, which you declare statically for your application) and every open view will have its CanExecute and Execute methods fired so that you can both vote in the closing of the application and also react to it, should it happen anyway. CompositeCommands are a feature of Prism. (See: Commanding Quickstart)

I think those are probably the most elegant. There's a few more options but these live in the best harmony with existing conventions.

Hope this helps.

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