WPF MVVM 在窗口关闭时调用 ViewModel Save 方法

发布于 2024-09-07 02:29:27 字数 395 浏览 3 评论 0原文

我已经弄清楚如何从我的 ViewModel 关闭窗口。

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

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

我正在考虑将 Command 属性绑定到 Window 的关闭事件,但我的 ViewModel 与用户控件相关,在这种情况下,关闭命令将不会被执行。

我也在看这个问题 Dispose WPF User Controls ,但我担心把Dispose 方法中的 Save 方法调用有点晚了。

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

非常感谢!

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

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

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

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.

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?

Thank You very much!

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

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

发布评论

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

评论(2

仅此而已 2024-09-14 02:29:27

如果您总是需要在关闭时保存,为什么不从 ViewModel 中的 Close() 方法而不是从 View 中调用 Save() 方法呢?然后只需确保窗口仅从 ViewModel 关闭,而不是从 View 本身关闭。类似的东西(未经测试,但你明白了)

public class SaveOnCloseViewModel
{
    public event Action RequestClose;
    ...
    public void Close()
    {
         Save();
         RequestClose();
    }
}

public class SaveOnCloseView
{
    private SaveOnCloseViewModel _vm;
    public SaveOnCloseView(SaveOnCloseViewModel vm)
    {
        _vm = vm;
        _vm.RequestClose += this.PerformClose;
        this.OnClosing += Window_Closing;
    }

    private bool _isClosing = false;
    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        if (!_isClosing)
        {
            e.Cancel = true;
            _vm.Close();
        }
    }

    private void PerformClose()
    {
        _isClosing = true;
        this.Close();
    }
}

If you always need to save on closing, why not call the Save() method from the Close() method in the ViewModel instead of from the View? Then just make sure that the window only closes from the ViewModel and not from the View itself. Something like (untested, but you get the idea)

public class SaveOnCloseViewModel
{
    public event Action RequestClose;
    ...
    public void Close()
    {
         Save();
         RequestClose();
    }
}

public class SaveOnCloseView
{
    private SaveOnCloseViewModel _vm;
    public SaveOnCloseView(SaveOnCloseViewModel vm)
    {
        _vm = vm;
        _vm.RequestClose += this.PerformClose;
        this.OnClosing += Window_Closing;
    }

    private bool _isClosing = false;
    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        if (!_isClosing)
        {
            e.Cancel = true;
            _vm.Close();
        }
    }

    private void PerformClose()
    {
        _isClosing = true;
        this.Close();
    }
}

海的爱人是光 2024-09-14 02:29:27

我认为最好的解决方案是在控件的事件中注册 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

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