当用户在表单上单击“下一步”按钮时,我想实现可能切换tabItems(TabItem1,TabItem2,TabItem3)的功能。因此我想使用State Machine Pattern。
这些我已经实施的东西,但我不知道,是不是?:
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 :(得分:1)
如果当你运行它时,你就可以正常运行,如果你运行它不起作用那么你就错了。
答案 1 :(得分:0)
一般来说:
您可以创建一个上下文类,其中包含所有相关状态以及当前,上一个和下一个状态。每个州都可以处理每个变更并根据以下内容决定下一个州: