【发布时间】:2015-05-01 17:03:25
【问题描述】:
我刚刚开始使用 PostSharp,目前用于提供撤消/重做功能,但将来可能会提供更多功能。
我想知道是否有任何方法可以在我们的应用程序中实现一组嵌套操作。
所以我在主应用程序中有我的对象模型,然后我启动一个编辑器窗口来修改消息列表。我可以正确地让 PostSharp 监控我对每条消息所做的所有单独更改,并允许用户撤消这些更改。到目前为止一切顺利。
然后我想要的是允许用户退出编辑器,现在在撤消堆栈上有一个操作 - “撤消消息更改”或类似的东西。这将涉及获取所有单独的更改并在编辑器被解雇时将它们合并到一个操作中。 我目前正在研究 OperationScope 方法,但我不确定我是否朝着正确的方向前进。当在作用域内时,操作不会立即添加到撤消堆栈中,它们仅在作用域关闭时才添加,这不是我想要的。
我已经包含了我的测试代码的一个非常精简的示例,以及一些关于我在不同点上的期望的 cmet。可能是我想要的东西是不可能的,但希望有人能提出一些建议!
private void btnShowEditor_Click(object sender, EventArgs e)
{
// Nothing on the undo stack at this point
LaunchEditorWindow();
// I would like one composite operation at this point that can undo all of the changes made within LaunchEditorWindow
}
// Imagine this code is inside an editor window and the changes are being done by the user
private void LaunchEditorWindow()
{
_network.Messages.Add(new Message() { Name = "Message 1", BitLength = 1 });
// I would expect there to be three undo operations available here
System.Diagnostics.Debug.WriteLine(RecordingServices.DefaultRecorder.UndoOperations.Count);
_network.Messages.Add(new Message() { Name = "Message 2", BitLength = 2 });
// 6 operations now
_network.Messages.Add(new Message() { Name = "Message 3", BitLength = 3 });
// etc etc
_network.Messages.Add(new Message() { Name = "Message 4", BitLength = 4 });
System.Diagnostics.Debug.WriteLine(RecordingServices.DefaultRecorder.UndoOperations.Count);
}
【问题讨论】: