WF4的设计器模型中提供了UndoEngine类,提供设计时的Undo(撤销)和Redo(重做)功能。我们可以调用工作设计器的服务来启用Undo和Redo功能。主要涉及到下面几个类:
下面一个简单例子说明
1.WPF的Xaml部分如下:
<Window x:Class="CaryUndo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sadt="clr-namespace:System.Activities.Presentation.Toolbox;assembly=System.Activities.Presentation"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="350" Width="525">
<Window.CommandBindings>
<CommandBinding Command="Undo" Executed="UndoHandle" />
<CommandBinding Command="Redo" Executed="RedoHandle" />
</Window.CommandBindings>
<Window.Resources>
<sys:String x:Key="AssemblyName">System.Activities, Version=
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="7*"/>
<ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>
<Border Grid.Column="0">
<sadt:ToolboxControl>
<sadt:ToolboxCategory CategoryName="Basic">
<sadt:ToolboxItemWrapper AssemblyName="{StaticResource AssemblyName}" >
<sadt:ToolboxItemWrapper.ToolName>
System.Activities.Statements.Sequence
</sadt:ToolboxItemWrapper.ToolName>
</sadt:ToolboxItemWrapper>
<sadt:ToolboxItemWrapper AssemblyName="{StaticResource AssemblyName}">
<sadt:ToolboxItemWrapper.ToolName>
System.Activities.Statements.WriteLine
</sadt:ToolboxItemWrapper.ToolName>
</sadt:ToolboxItemWrapper>
<sadt:ToolboxItemWrapper AssemblyName="{StaticResource AssemblyName}">
<sadt:ToolboxItemWrapper.ToolName>
System.Activities.Statements.Pick
</sadt:ToolboxItemWrapper.ToolName>
</sadt:ToolboxItemWrapper>
</sadt:ToolboxCategory>
</sadt:ToolboxControl>
</Border>
<Border Grid.Column="1" Name="DesignerBorder"></Border>
<Border Grid.Column="2" Name="PropertyBorder"/>
<Button Content="Undo" Command="Undo" Height="23" HorizontalAlignment="Left" Margin="9,179,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
<Button Content="Redo" Command="Redo" Height="23" HorizontalAlignment="Left" Margin="9,218,0,0" Name="button2" VerticalAlignment="Top" Width="75" />
</Grid>
</Window>
2.后台代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Activities.Presentation;
using System.Activities.Core.Presentation;
using System.Activities.Statements;
namespace CaryUndo
{
public partial class MainWindow : Window
{
WorkflowDesigner wd = new WorkflowDesigner();
UndoEngine undoEngineService;
public MainWindow()
{
InitializeComponent();
}
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
// register metadata
(new DesignerMetadata()).Register();
// create the workflow designer
wd.Load(new Sequence());
DesignerBorder.Child = wd.View;
PropertyBorder.Child = wd.PropertyInspectorView;
this.undoEngineService = this.wd.Context.Services.GetService<UndoEngine>();
this.undoEngineService.UndoUnitAdded += new EventHandler<UndoUnitEventArgs>(OnUndoUnitAdded);
}
void OnUndoUnitAdded(object sender, UndoUnitEventArgs e)
{
MessageBox.Show("Undo Unit");
}
private void UndoHandle(object sender, ExecutedRoutedEventArgs e)
{
this.wd.Context.Services.GetService<UndoEngine>().Undo();
}
private void RedoHandle(object sender, ExecutedRoutedEventArgs e)
{
this.wd.Context.Services.GetService<UndoEngine>().Redo();
}
}
}
3.我们在自己重新宿主的设计器,增加几个活动后,我们可以支持多步的Undo和Redo动作。如下图: