WPF MVVM在窗口关闭时调用ViewModel Save方法 [英] WPF MVVM call ViewModel Save method on Window Close

查看:354
本文介绍了WPF MVVM在窗口关闭时调用ViewModel Save方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想出了如何从ViewModel中关闭Window.

I have figured out how to close Window from my ViewModel.

现在我需要从另一侧解决窗口关闭问题.

Now I need to solve window close problem from the other side.

当用户单击窗口的关闭按钮时,我需要在ViewModel中触发Save()方法.

I need to trigger Save() method in my ViewModel when the user clicks window's close button.

我正在考虑将Command属性绑定到Window的close事件,但是我的ViewModel与用户控件有关,在这种情况下,将不会执行Close命令.

I was thinking about binding Command property to Window's close event, but my ViewModel is related to the user control and in that case Close command will not be executed.

我也在看这个问题部署WPF用户控件,但是我担心将Save方法调用放到Dispose方法中为时已晚.

I was looking at this question as well Disposing WPF User Controls , but I'm afraid that putting Save method call in Dispose method is a little bit too late.

有什么办法可以解决这个问题?

Is there any way to solve this problem?

非常感谢您!

推荐答案

我认为最好的解决方案是在控件的事件中注册ViewModel,该事件也在窗口的关闭事件中注册.这样,您将来也可以在windowClosed事件中注册其他ViewModel,以引发其他ViewModel中的其他save()方法.

I think the best solution for this would be to register the ViewModel at an event of your control which is also registered at the closed event of you window. This way you can also register other ViewModels at the windowClosed Event in the future to raise other save() Methods in other ViewModels

这篇关于WPF MVVM在窗口关闭时调用ViewModel Save方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆