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动作。如下图:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?