博客园  :: 首页  :: 联系 :: 订阅 订阅  :: 管理

WF4工作流设计器模型对Undo和Redo的支持

Posted on 2010-01-05 20:45  生鱼片  阅读(2448)  评论(2编辑  收藏  举报

WF4的设计器模型中提供了UndoEngine类,提供设计时的Undo(撤销)和Redo(重做)功能。我们可以调用工作设计器的服务来启用UndoRedo功能。主要涉及到下面几个类:

clip_image002

下面一个简单例子说明

1.WPFXaml部分如下:

<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=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35</sys:String>

    </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.我们在自己重新宿主的设计器,增加几个活动后,我们可以支持多步的UndoRedo动作。如下图:

clip_image004