Siamo giunti al
livello presentation.
Creiamo un nuovo progetto di tipo
Windows Application e lo chiamiamo
SimWFCallCenterPresentation, aggiungiamo i riferimenti ai nostri due progetti
SimWFCallCenterActivityInterface e
SimWFCallCenterWorkflow e ai soliti
System.Workflow.Activities,
System.Workflow.ComponentModel,
System.Workflow.Runtime.
Creiamo poi una windows form e chiamiamola
frmPresentation; per brevità allego l’immagine di ciò che contiene visivamente al suo interno:
<img src="http://farm4.static.flickr.com/3280/2935391150_5125db7b9c_o.png" width="390" height="374" alt="AppFormCurrent05" />
Nel
code-behind, nella sezione
using, inseriamo:
<span style="font-size:1.0em">
using System.Workflow.Runtime;
using System.Workflow.Activities;
</span>
e nella
partial class della form:
<span style="font-size:1.0em">
private WorkflowRuntime wfRuntime;
private SimWFCallCenterActivityInterface.CallCenterService callCenterService;
private StateMachineWorkflowInstance stateMachineInstance;
</span>
La prima dichiarazione è relativa al runtime di WF, la seconda è alla nostra classe di servizio pubblico e la terza è l’istanza di un workflow di tipo “macchina a stati”.
A seguire inseriamo i seguenti
delegate:
<span style="font-size:1.0em">
private delegate void UpdateMenuItemDelegate(WorkflowInstance wfInstance, string wfState, string wfStatus);
private delegate void UpdateButtonStatusDelegate();
private delegate void UpdateMenuTextDelegate(object sender, SimWFCallCenterActivityInterface.UpdateMenuEventArgs e);
</span>
Il primo si occuperà di aggiornare i contenuti del menu di navigazione, il secondo aggiornerà lo stato dei pulsanti, che saranno quindi sensibili al contesto, e il terzo aggiornerà i contenuti degli argomenti selezionati. Ora aggiungiamo la seguente
property, che ci servirà per passare il testo giusto alla
RichTextBox, che rappresenta l’area di lettura dei menu;
rtbDisplay è la
RichTextBox:
<span style="font-size:1.0em">
public string Dialogs
{ get
{ return this.rtbDisplay.Text;
}
set
{ this.rtbDisplay.Text = value;
}
}
</span>
Creiamo finalmente il metodo, che farà partire il runtime di WF impostato con i servizi del nostro workflow (i commenti nel codice sono esplicativi):
<span style="font-size:1.0em">
private void StartWorkFlow()
{ // Crea una nuova istanza del workflow runtime
wfRuntime = new WorkflowRuntime();
// Crea gli eventhandlers per il runtime
wfRuntime.WorkflowTerminated += new EventHandler(wfRuntime_WorkflowTerminated);
wfRuntime.WorkflowCompleted += new EventHandler(wfRuntime_WorkflowCompleted);
wfRuntime.WorkflowIdled += new EventHandler(wfRuntime_WorkflowIdled);
// Esegue il servizio
wfRuntime.StartRuntime();
// Aggiunge una instanza dell' External Data Exchange Service
ExternalDataExchangeService extDataExchangeService = new ExternalDataExchangeService();
wfRuntime.AddService(extDataExchangeService);
// Aggiunge una nuova istanza del callcenterService al servizio external data exchange
callCenterService = new SimWFCallCenterActivityInterface.CallCenterService();
extDataExchangeService.AddService(callCenterService);
callCenterService.MenuChangedEventHandler +=new SimWFCallCenterActivityInterface.CallCenterService.UpdateMenuEventHandler(callCenterService_MenuChangedEventHandler);
}
</span>
e implementiamo il metodo
StartCallCenterWorkflow(), che creerà una istanza del nostro workflow di tipo
StateMachineWorkflow:
<span style="font-size:1.0em">
private StateMachineWorkflowInstance StartCallCenterWorkflow()
{ WorkflowInstance wi = this.wfRuntime.CreateWorkflow(typeof(SimWFCallCenterWorkflow.CallCenterWorkflow));
wi.Start();
return new StateMachineWorkflowInstance(this.wfRuntime, wi.InstanceId);
}
</span>
Ora facciamo doppio clic sul pulsante
Inizia Sessione e inseriamo il seguente codice nell’evento del clic:
<span style="font-size:1.0em">
private void butStart_Click(object sender, EventArgs e)
{ stateMachineInstance = this.StartCallCenterWorkflow();
}
</span>
Creiamo un evento clic, che assoceremo a tutti i pulsanti di selezione menu:
<span style="font-size:1.0em">
private void butSingle_Click(object sender, EventArgs e)
{ string name = ((Button)sender).Name;
this.DisableBut();
callCenterService.RaiseMenuItemSelected(new SimWFCallCenterActivityInterface.CallCenterEventArgs(stateMachineInstance.InstanceId, name));
}
</span>
DisableBut() è un metodo che imposta disabilita tutti i pulsanti di selezione.