当前位置: 首页>>代码示例>>C#>>正文


C# StateMachine.AddEnterStateCallback方法代码示例

本文整理汇总了C#中StateMachine.AddEnterStateCallback方法的典型用法代码示例。如果您正苦于以下问题:C# StateMachine.AddEnterStateCallback方法的具体用法?C# StateMachine.AddEnterStateCallback怎么用?C# StateMachine.AddEnterStateCallback使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在StateMachine的用法示例。


在下文中一共展示了StateMachine.AddEnterStateCallback方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Test1

        public void Test1()
        {
            #region Creating the state machine instance called machine
            var machine = new StateMachine<States>();

            machine.AddState(States.R);
            machine.AddState(States.G);
            machine.AddState(States.B);

            machine.AddTransition(States.R, States.G);
            machine.AddTransition(States.G, States.B);
            #endregion

            string EnteringCallbackMsg = "", ExitingCallbackMsg = "", TransistionCallback = "";
            string TransitingEventMsg = "", TransitedEventMsg = "";

            #region Adding callbacks to machine
            Action<States> EnteringCallback = (state) => EnteringCallbackMsg = String.Format("Entering state {0}", state);
            machine.AddEnterStateCallback(States.R, EnteringCallback.Invoke);
            machine.AddEnterStateCallback(States.G, EnteringCallback.Invoke);
            machine.AddEnterStateCallback(States.B, EnteringCallback.Invoke);

            Action<States> ExitingCallback = (state) => ExitingCallbackMsg = String.Format("Exiting state {0}", state);
            machine.AddExitStateCallback(States.R, ExitingCallback.Invoke);
            machine.AddExitStateCallback(States.G, ExitingCallback.Invoke);
            machine.AddExitStateCallback(States.B, ExitingCallback.Invoke);

            Action<States, States> TransitionCallback = (from, to) => TransistionCallback = String.Format("Going from {0} to {1}", from, to);
            machine.AddTransitionCallback(States.R, States.G, TransitionCallback.Invoke);
            machine.AddTransitionCallback(States.G, States.B, TransitionCallback.Invoke);
            #endregion

            #region Adding events to machine
            machine.Transiting += new EventHandler<TransitingEventArgs<States>>((s, e) => TransitingEventMsg = String.Format("Event Fired: Transiting into state {0}", e.State));
            machine.Transited  += new EventHandler<TransitedEventArgs<States>>( (s, e) => TransitedEventMsg  = String.Format("Event Fired: Transited into state {0}", e.State));
            #endregion

            #region Testing machine
            Assert.AreEqual(machine.CurrentState, null);

            machine.GoToState(States.R);
            Assert.AreEqual(machine.CurrentState.Name, States.R);
            Assert.AreEqual(ExitingCallbackMsg, "");
            Assert.AreEqual(TransistionCallback, "");
            Assert.AreEqual(EnteringCallbackMsg, "Entering state R");
            Assert.AreEqual(TransitingEventMsg, "Event Fired: Transiting into state R");
            Assert.AreEqual(TransitedEventMsg, "Event Fired: Transited into state R");

            machine.GoToState(States.G);
            Assert.AreEqual(machine.CurrentState.Name, States.G);
            Assert.AreEqual(ExitingCallbackMsg, "Exiting state R");
            Assert.AreEqual(TransistionCallback, "Going from R to G");
            Assert.AreEqual(EnteringCallbackMsg, "Entering state G");
            Assert.AreEqual(TransitingEventMsg, "Event Fired: Transiting into state G");
            Assert.AreEqual(TransitedEventMsg, "Event Fired: Transited into state G");

            machine.GoToState(States.B);
            Assert.AreEqual(machine.CurrentState.Name, States.B);
            Assert.AreEqual(ExitingCallbackMsg, "Exiting state G");
            Assert.AreEqual(TransistionCallback, "Going from G to B");
            Assert.AreEqual(EnteringCallbackMsg, "Entering state B");
            Assert.AreEqual(TransitingEventMsg, "Event Fired: Transiting into state B");
            Assert.AreEqual(TransitedEventMsg, "Event Fired: Transited into state B");
            #endregion
        }
开发者ID:sdenel,项目名称:StateMachine,代码行数:65,代码来源:Program.cs


注:本文中的StateMachine.AddEnterStateCallback方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。