兽人.Memento全局撤销与多个控制



我需要实现一个memento-undo-redo模式。我的应用程序有多个选项卡,在这些选项卡中有多个控件,它们都实现了Orc.Mento。我遇到的问题是用主窗口上的菜单按钮调用undo,并在按钮操作中调用最后一个活动控件上的undo。

编辑:不幸的是,这个项目没有遵循MVVM。

我选择Orc.Memento是因为它非常容易在不修改对象的情况下实现。我现在只有使用键盘命令Ctrl+X&Ctrl+Y。调用undo仅对活动控件执行undo操作。然而,当我单击MainWindow菜单上的undo/redo按钮时,我的代码不知道调用undo/redon的最后一个活动控件。

选项1

选项一是通过在每个控件的GotFocus()上设置全局属性来跟踪最后一个活动控件。我觉得必须有更好的方法。

选项2

这就是我在这里的原因:-(。


控制

public class MyControl : IMemento
{
private MementoService mementoService = new MementoService();
public void RegisterAll()
{
mementoService.RegisterObject(myObject);
mementoService.RegisterCollection(myCollection);
}
public void Undo()
{
mementoService.Undo();
}
public void Redo()
{
mementoService.Redo();
}
}

主窗口

Ctrl+Z&此处映射了Ctrl+Y。undo/redo方法找到当前活动的控件,并在该控件上调用undo/reddo。

public MainWindow
{   
/// <summary>
/// Call undo on the currently active control
/// </summary>
public void Undo()
{
/*
* get current focused control.
* find the parent that is an IMemento. And call Redo on that control
*/
var focusedControl = FocusManager.GetFocusedElement(this);
var mementoControl = UIHelper.TryFindParentThatIsIMemento<Control>(focusedControl as DependencyObject);
/*
* Call Undo on the control that is currently active
*/
if (mementoControl != null && mementoControl is IMemento)
{
var mem = (mementoControl as IMemento);
mem.Undo();
}
}
}

注意:如果我能通过自动导航到发生撤消/重做的控件来编程Excel的工作方式,那就太好了。没有必要,但如果你有主意的话,我会洗耳恭听的。

以下是一些建议:

  1. 尝试针对模型(例如使用Orc.ProjectManagement(而不是针对视图(因为视图是短暂的

  2. 尝试使用Orc.Controls中的TabControl,它允许您保持所有选项卡处于活动状态,从而允许重做/撤消(。

最新更新