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 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器