閱讀: 602 評論: 0 作者: 生魚片 發表于 2009-12-07 21:24 原文鏈接

標題的兩者并沒有什么關系,只是內容都比較短就放到一起了。

1.在工作流中存儲ModelItems的視圖狀態。

例子,   我們創建一個自定義活動的活動設計器ActivityDesigner1.XAML,如下:

<sap:ActivityDesigner x:Class="CaryActivityLibrary1.ActivityDesigner1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:sap="clr-namespace:System.Activities.Presentation;assembly=System.Activities.Presentation"

    xmlns:sapv="clr-namespace:System.Activities.Presentation.View;assembly=System.Activities.Presentation">

    <Grid>

        <StackPanel Name="stackPanel1" VerticalAlignment="Top" >

            <TextBox  Name="commentBlock"   />

            <Button Content="Load View State" Name="loadViewState" Click="loadViewState_Click" />

            <Button Content="Commit View State"  Name="button1" Click="button1_Click" />

        </StackPanel>

    </Grid>

</sap:ActivityDesigner>

 

對應的代碼如下:

private void loadViewState_Click(object sender, RoutedEventArgs e)

        {

            ViewStateService vss = this.Context.Services.GetService<ViewStateService>();

            commentBlock.Text = vss.RetrieveViewState(this.ModelItem, "comment") as string;

        }

 

        private void button1_Click(object sender, RoutedEventArgs e)

        {

            ViewStateService vss = this.Context.Services.GetService<ViewStateService>();

            vss.StoreViewStateWithUndo(this.ModelItem, "comment", commentBlock.Text);

        }

我們在工作流中使用該活動,輸入一些信息后提交ViewState,你會看到工作流中增加了如下部分:

<sap:WorkflowViewStateService.ViewState>

      <scg3:Dictionary x:TypeArguments="x:String, x:Object">

        <x:String x:Key="comment">123456</x:String>

      </scg3:Dictionary>

    </sap:WorkflowViewStateService.ViewState>

2.補償

補償是提供給前面已經成功的操作一個補償的機會,WF4提供了下面活動支持補償:

image
1. CompensableActivity活動,該活動調用后會返回一個CompensationToken類型的Result屬性,
2. Compensate活動和Confirm活動用于顯示的調用CompensableActivity活動的confirmation handler 和 compensation handler部分。這兩個活動都有一個Target屬性用于指定CompensableActivity活動的Result屬性。
3. CompensableActivity活動的CompenationHandler當工作流有異常時調用,ConfirmationHandler為工作流正常執行完成后調用不是CompensableActivity活動執行完成就立即調用,CancellationHandler為活動取消時調用。

參考資源:

1.http://blogs.msdn.com/mwinkle/archive/2009/12/06/wf4-viewstateservice.aspx
2.http://msdn.microsoft.com/en-us/library/system.activities.presentation.view.viewstateservice_members(VS.100).aspx
3.http://msdn.microsoft.com/en-us/library/dd489415(VS.100).aspx

  發表評論


新聞頻道:Mono發布2.4.3版本,支持C# 4.0的所有功能

推薦鏈接:Windows 7專題發布

網站導航:博客園首頁  個人主頁  新聞  社區  博問  閃存  知識庫


文章來源:http://www.cnblogs.com/carysun/archive/2009/12/07/wf4-viewstateservice-compensate.html