我觉得需要一个完整的限定示例来使用
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
注意,在上面的代码中,我使用了一个字符串变量来简化事情,但我需要保存和恢复
控制