6

在 WPF 中 App.Current.SessionEnding 必须在几秒钟内返回,否则会出现“应用程序不响应”窗口。所以不能在这个事件处理程序中要求用户保存他的数据,因为用户的响应时间超过几秒钟。

我认为一个解决方案是取消注销/关闭/重新启动,并在用户回答文件保存对话框时恢复它。

    ReasonSessionEnding _reasonSessionEnding;

    App.Current.SessionEnding +=
        new SessionEndingCancelEventHandler(Current_SessionEnding);

    void Current_SessionEnding(object sender, SessionEndingCancelEventArgs e)
    {
        if (_dataModified)
        {
            e.Cancel = true;
            _reasonSessionEnding = e.ReasonSessionEnding;
            Dispatcher.CurrentDispatcher.BeginInvoke(new Action(EndSession));
        }
    }

    void EndSession()
    {
        if (SaveWithConfirmation()) // if the user didn't press Cancel
            //if (_reasonSessionEnding = ReasonSessionEnding.Logoff)
                // logoff
            //else
                // shutdown or restart ?
    }

问题是 ReasonSessionEnding 没有告诉我 Windows 是关闭还是重新启动(它不区分两者)。

那么,我的程序在会话结束事件上应该做什么?它甚至应该做任何事情,还是在这个事件上什么都不做是标准?

要求用户将他的更改保存在我的主窗体的 OnClosing 方法中,因此他不会丢失数据,但我认为“应用程序不响应”窗口并不表示正常的工作流程。

我猜不希望取消关闭,因为其他一些程序已经关闭。

4

1 回答 1

6

似乎被接受的方式是无论如何都显示另存为对话框。

由于您陈述的原因和其他各种原因,取消关闭,然后再恢复它肯定不是一种选择。

由于简单地丢弃数据是不可接受的,因此确实没有其他选择。

好吧,除了将数据保存到临时文件,然后在下次运行程序时自动恢复它们。更像是崩溃后的 MS Word。实际上,我考虑得越多,听起来就越好。

Edit: There's yet another avenue, namely to save continously, the way eg. MS OneNote does. What has struck me before is that, provided you implement decent multilevel undo in your application, the whole manual saving business is actually somewhat dated - an anachronism from the days when disk operations were expensive and error-prone, nowadays mostly old habit.

But I'm digressing. Anyway, it's probably not applicable to your application, since I imagine it needs to be implemented from the beginning.

于 2009-03-11T00:45:15.000 回答