2009-10-14 192 views
0

當用戶單擊窗體上的「下一個」按鈕時,我想要實現可能切換tabItem(TabItem1,TabItem2,TabItem3)的功能。爲此,我想使用狀態機模式。WPF和狀態機模式

這些事我已經實現,但我不知道,是不是正確與否?:

public abstract class State 
    { 
     #region Constructros 
      public State() 
     { 
     } 


     public State(State state) 
     { 
      this.CurrentState = state.CurrentState; 
      this.PreviousState = state.PreviousState; 
      this.NextState = state.NextState; 
     } 


     public State(Machine machine,string strCurrentState,string strPreviousState,string strNextState) 
     { 
      this.CurrentState = strCurrentState; 
      this.PreviousState = strPreviousState; 
      this.NextState = strNextState; 
      this.SetParams(); 
     } 
     #endregion 

     public const string WELCOME    = "WELCOME"; 
     public const string EMR_CONFIGURATION = "EMR_CONFIGURATION"; 
     public const string MIGRATION   = "MIGRATION"; 
     public const string END_OF_STATES  = "END_OF_STATES"; 


     private Machine machine; 
     private string currentState; 
     private string previousState; 
     private string nextState; 
     private string nameState; 

     public virtual void SetParams() { } 
     public virtual void ChangeState(Machine m, State s) 
     { 
      m.ChangeState(s); 
     } 


     //Get The name of State 
     public Machine Machine 
     { 
      get { return this.machine; } 
      set { this.machine = value; } 
     } 

     //Current State 
     public string CurrentState 
     { 
      get { return this.currentState; } 
      set { this.currentState = value; } 
     } 
     //Previous State 
     public string PreviousState 
     { 
      get { return this.previousState; } 
      set { this.previousState = value; } 
     } 
     //Next State 
     public string NextState 
     { 
      get { return this.nextState; } 
      set { this.nextState = value; } 
     } 

     public string NameState 
     { 
      get { return this.nameState; } 
      set { this.nameState = value; } 
     } 

    } 

public class Machine 
    { 
     public State currentState; 

     public Machine (string strCurrentState,string strPreviousState,string strNextState) 
     { 
      currentState = new WelcomeState(this, strCurrentState, strPreviousState, strNextState); 
     } 

     public void ChangeState(State setState) 
     { 
      currentState = setState; 
     } 

     public void SetCurrentState (string state) 
     { 
      currentState.CurrentState = state; 
     } 
    } 

class Transition 
    { 
     public State state; 
     private static Transition getInstance; 
     protected Transition(){} 

     public static Transition GetInstance() 
     { 
      if (getInstance == null) 
      { 
       getInstance = new Transition(); 
      } 
      return getInstance; 
     } 

     public void Transform(State state) 
     { 
      if (state == null) 
      { 
       return; 
      } 

      // Get the type of state. 
      string stateType = state.GetType().Name; 


      // WELCOME TabItem 
      if (state.CurrentState == State.WELCOME) 
      { 
       state.ChangeState(state.Machine, new WelcomeState(state)); 
      } 

      //EMR_CONFIGURATION TabItem 
      if (state.CurrentState == State.EMR_CONFIGURATION) 
      { 
       state.ChangeState(state.Machine, new EMRConfigurationState(state)); 
      } 

      //MIGRATION TabItem 
      if (state.CurrentState == State.EMR_CONFIGURATION) 
      { 
       state.ChangeState(state.Machine, new MigrationState(state)); 
      } 

     } 
    } 

    public class WelcomeState : State 
    { 
     public WelcomeState(State state) : base(state) 
    { 
     } 

     public WelcomeState (Machine machine, string strCurrentState,string strPreviousState,string strNextState): 
      base(machine, strCurrentState, strPreviousState, strNextState) 
      { 
      } 


     public override void SetParams() 
    { 
      this.CurrentState = State.WELCOME; 
      this.PreviousState = State.WELCOME; 
      this.NextState  = State.EMR_CONFIGURATION; 
    } 
    } 

public class EMRConfigurationState : State 
    { 
     public EMRConfigurationState(State state) : base(state) 
    { 
     } 

     public EMRConfigurationState(Machine machine, string strCurrentState, string strPreviousState, string strNextState) : 
      base(machine, strCurrentState, strPreviousState, strNextState) 
      { 
      } 

     public override void SetParams() 
    { 
      this.CurrentState = State.EMR_CONFIGURATION; 
      this.PreviousState = State.WELCOME; 
      this.NextState  = State.MIGRATION; 
    } 
    } 

public class MigrationState: State 
    { 
     public MigrationState(State state) : base(state) 
    { 
     } 

     public MigrationState(Machine machine, string strCurrentState, string strPreviousState, string strNextState) : 
      base(machine, strCurrentState, strPreviousState, strNextState) 
      { 
      } 

     public override void SetParams() 
    { 
      this.CurrentState = State.MIGRATION; 
      this.PreviousState = State.EMR_CONFIGURATION; 
      this.NextState  = State.END_OF_STATES; 
    } 
    } 


public partial class Window1 : Window 
    { 
     private WizardPresenter wizPresenterWelcome = null; 
     public List<WizardPresenter> stateList; 
     public Window1() 
     { 
      InitializeComponent(); 

      stateList = new List<WizardPresenter> 
             { 
              new WizardPresenter(StatePresenter.WELCOME, 
               StatePresenter.WELCOME, 
               StatePresenter.EMR_CONFIGURATION), 

              new WizardPresenter(StatePresenter.EMR_CONFIGURATION, 
               StatePresenter.WELCOME,StatePresenter.MIGRATION), 

              new WizardPresenter(StatePresenter.MIGRATION, 
               StatePresenter.EMR_CONFIGURATION, 
               StatePresenter.WELCOME), 
             }; 

      tabControl.ItemsSource = stateList; 
     } 

     private WizardPresenter GetWizardPresenter(string strState) 
     { 
      foreach (WizardPresenter presenter in stateList) 
      { 
       if (presenter.currentStatePresenter.CurrentStatePresenter == strState) 
       { 
        return presenter; 
        break; 
       } 
      } 
      return null; 
     } 

     private void button_Click(object sender, RoutedEventArgs e) 
     { 
      if (this.wizPresenterWelcome == null) 
      { 
       try 
       { 
        { 
         WizardPresenter wp = (WizardPresenter)tabControl.SelectedItem; 
         string nextState = wp.currentStatePresenter.NextStatePresenter; 

         tabControl.SelectedIndex = stateList.IndexOf(GetWizardPresenter(nextState)); 
        } 
       } 
       catch (Exception ex) 
       { 
        MessageBox.Show("Error:" + ex.Message + "Error"); 
       } 
      } 
     } 
    } 
+0

嚴重,要求我們通過與模糊的所有代碼涉水「我已經得到它嗎?」幾乎不是真正的問題。 – AnthonyWJones 2009-10-14 08:06:21

+0

我只想聽聽你的意見,我是對的邏輯還是沒有。 我已經把我所有的代碼放在了一起,讓你們有可能在出現問題時對它進行破譯。 我沒有足夠的經驗,這就是爲什麼我想在這裏得到更有經驗的技術人員的支持。 謝謝, 朱利安 – julianusti 2009-10-14 08:13:08

回答

1

如果當你運行它的作品,那麼你知道了吧,如果當你運行它不起作用,那麼你錯了。

+0

你對:)我試過,它不起作用,可能你可以給我一個線索爲什麼? :)) 謝謝。 朱利安 – julianusti 2009-10-14 08:14:46

+0

好吧,讓我重述一下我的問題! 如何使簡單的狀態機,這使我可以切換Tabitems,當用戶按下一步按鈕?我需要知道currentState,previousState和nextState。 每個國家都是單身。 抽象類國家(){ 國家 (字符串名稱)//國家 {} 名} 類機(){ } 謝謝。 – julianusti 2009-10-14 08:30:24

0

一般:

  1. 當你改寫一個問題,修改原來的職位,並添加說明那裏。
  2. 您可以創建一個上下文類,該上下文類包含所有相關狀態以及當前,上一個和下一個狀態。每個狀態可以處理每個更改並根據以下內容決定下一個狀態:

    • 上下文變量的運行時值。
    • 上下文的先前狀態。