代码之家  ›  专栏  ›  技术社区  ›  ElektroStudios

使用“系统”的示例。活动。声明。C#或VB中的StateMachine类。净额

  •  3
  • ElektroStudios  · 技术社区  · 8 年前

    我觉得需要一个完整的限定示例来使用 System.Activities.Statements.StateMachine Control 实例(这意味着,我想存储控件的状态,以替换基于控件属性和“命令”的典型撤消/重做方法)。

    我一直在谷歌上搜索自制的状态机实现,我发现了很多,但后来我在中发现了微软的这个实现。NET Framework类库(那么现在我没有理由依赖任何自制实现来重新发明轮子),然而,MSDN没有代码示例来介绍该类的使用,我在谷歌上没有找到任何证明其用法的结果(以及我在MSDN中看到的一些其他相关成员的代码示例,我根本不明白该如何使用它们)。

    现在我真的是瞎了,试着通过试错来学习如何使用那个类及其相关成员。

    obj 变量,具体来说是在 System.Activities.OutArgument.Set System.Activities.OutArgument.Get 需要 ActivityContext 无效的 ).

    C#(由在线代码转换器翻译,可能有语法错误):

    using System.Activities;
    using System.Activities.Statements;
    
    // Create a container for statemachines
    StateMachine sm = new StateMachine();
    
    // Declare an object to test its state...
    string obj = null;
    
    // Modify the state of 'obj'.
    obj = "hello";
    
    // Save 1st state of 'obj'.
    Activity<string> act1 = obj;
    act1.Result = new OutArgument<string>();
    act1.Result.Set(null, obj);
    State state1 = new State();
    state1.Entry = act1;
    
    // Modify the state of 'obj'.
    obj = "hello world";
    
    // Save 2nd state of 'obj'.
    Activity<string> act2 = obj;
    act2.Result = new OutArgument<string>();
    act2.Result.Set(null, obj);
    State state2 = new State();
    state2.Entry = act2;
    
    // Add saved states into the statemachine collection.
    sm.States.Add(state1);
    sm.States.Add(state2);
    
    // Modify the state of 'obj'.
    obj = string.Empty;
    
    // Restore last saved state of 'obj'.
    obj = ((Activity<string>)sm.States.Last().Entry).Result.Get(null);
    

    VB。净值:

    Imports System.Activities
    Imports System.Activities.Statements
    
    ' Create a container for statemachines
    Dim sm As New StateMachine()
    
    ' Declare an object to test its state...
    Dim obj As String
    
    ' Modify the state of 'obj'.
    obj = "hello"
    
    ' Save 1st state of 'obj'.
    Dim act1 As Activity(Of String) = obj
    act1.Result = New OutArgument(Of String)
    act1.Result.Set(Nothing, obj)
    Dim state1 As New State()
    state1.Entry = act1
    
    ' Modify the state of 'obj'.
    obj = "hello world"
    
    ' Save 2nd state of 'obj'.
    Dim act2 As Activity(Of String) = obj
    act2.Result = New OutArgument(Of String)
    act2.Result.Set(Nothing, obj)
    Dim state2 As New State()
    state2.Entry = act2
    
    ' Add saved states into the statemachine collection.
    sm.States.Add(state1)
    sm.States.Add(state2)
    
    ' Modify the state of 'obj'.
    obj = String.Empty
    
    ' Restore last saved state of 'obj'.
    obj = DirectCast(sm.States.Last().Entry, Activity(Of String)).Result.Get(Nothing)
    

    ...还有一些说明,如 Activity<string> act1 = obj; Dim act1 As Activity(Of String) = obj

    注意,在上面的代码中,我使用了一个字符串变量来简化事情,但我需要保存和恢复 控制

    1 回复  |  直到 8 年前
        1
  •  6
  •   Peter Bons    8 年前

    这个 System.Activities 命名空间属于 Windows Workflow Foundation SharePoint workflows 例如

    这个 StateMachine 类是多个子活动的容器,这些子活动共同构成一个 State Machine Workflow

    定义工作流后,您可以使用工作流引擎运行它 WorkflowInvoker 或者 WorkflowApplication . 从 docs :

    这个 ActivityContext

    你找不到太多使用 类是指大多数工作流是使用创建的图形设计器构建的。XAML文件和我不认为许多是只使用代码构建的。

    有些框架/库可能更适合,例如 this

    推荐文章