예제 #1
0
        public async Task ReactiveCommandTest()
        {
            var c = new ReactiveCommand(true);

            var v = false;

            c.CanExecuteChanged         += (o, e) =>
                                       v = c.CanExecute(null);
            c.ListenCanExecuteObservable(Observable.Delay(Observable.Range(0, 1).Select(xxx => false), DateTimeOffset.Now.AddMilliseconds(15)));
            Assert.AreEqual(c.CanExecute(null), true);
            await Task.Delay(100);

            Assert.AreEqual(v, false);
        }
예제 #2
0
        public Game <WhoIsBiggerContext> Create()
        {
            var game = GameBuilder.Create <WhoIsBiggerContext>()
                       //.HasOneOfCommands(
                       //	"UserInput",g=>
                       //	{

                       //	},
                       //	g=>
                       //	{

                       //	}
                       //)
                       .HasCommand("StartGame", g =>
            {
                var cmd = new ReactiveCommand();
                cmd.ListenCanExecuteObservable(g
                                               .GetValueContainer(x => x.CurrentState)
                                               .GetNullObservable()
                                               .Select(_ => g.CurrentState.Name == GameStateNames.GameStart));

                cmd.Subscribe(e => g.CurrentState = g.AllStates[GameStateNames.UserInput]);
                return(cmd);
            })
                       .HasCommand("PlayerInput", g =>
            {
                var cmd = new ReactiveCommand();

                cmd.ListenCanExecuteObservable(g
                                               .GetValueContainer(x => x.CurrentState)
                                               .GetNullObservable()
                                               .Select(_ =>
                                                       g.CurrentState.Name == GameStateNames.UserInput
                                                       ||
                                                       g.CurrentState.Name == GameStateNames.GameSet
                                                       ));

                cmd.Subscribe(e =>
                {
                    var input = (decimal)e.EventArgs.EventArgs;
                    g.GameExecutingContext.UserInputs.Add(input);
                    g.GameExecutingContext.CurrentUserIndex++;
                }
                              );
                return(cmd);
            })
                       .HasStartState("GameStart", "Game Start")
                       .EndHasState()

                       .HasState(GameStateNames.UserInput)
                       .HasCheckerForContextDataChange(c => c.CurrentUserIndex != c.UserInputs.Count)
                       .ThenDo(c => c.CurrentUserIndex = c.UserInputs.Count)

                       .HasCheckerForContextDataChange(c => true)
                       .ThenDo(c =>
            {
                var msg = string.Format("{0}'s  input is {1}", c.Users[c.CurrentUserIndex], c.UserInputs[c.CurrentUserIndex]);
                c.Messages.Add(msg);
                c.LastMessages.Add(msg);
            })

                       .HasCheckerForContextDataChange(c => c.LastMessages.Count > c.MaxLastMessageCount)
                       .ThenDo(c =>
            {
                while (c.LastMessages.Count > c.MaxLastMessageCount)
                {
                    c.LastMessages.RemoveAt(0);
                }
            })

                       .HasCheckerForStateChange(c => c.UserInputs.Count == c.Users.Count)
                       .ThenGoToState(GameStateNames.GameSet)
                       .EndHasState()
                       .HasEndState(GameStateNames.UserInput)
                       .EndHasState()

                       .WithContext(new WhoIsBiggerContext
            {
                Users = new System.Collections.ObjectModel.ObservableCollection <string>()
                {
                    "U1",
                    "U2"
                }
            })
                       .CurrentProduct;

            return(game);
        }