程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> WF4工作流設計器模型對Undo和Redo的支持

WF4工作流設計器模型對Undo和Redo的支持

編輯:關於.NET

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=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.我們在自己重新宿主的設計器,增加幾個活動後,我們可以支持多步的Undo和Redo動作。如下圖:

出處:http://carysun.cnblogs.com/

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved