Using the Events Manager of the InfoPath Hosted Control

來源:互聯網
上載者:User

The InfoPath hosted control gives developers of third party hosted applications the ability to respond to events in the form.  The InfoPath event manager provides this functionality.  Through the event manager a host application can respond to 5 form events, 3 xml events and 1 control event:

Form Events

Xml Events

Control Events

Saving event
Context Changed event
Sign event
Merging event
View Switched event

Changing event
Validating event
Changed event

Clicking event

How can third party host applications use the event manager?

Step 1: Implement an InternalStartup method

First, add code to handle the InternalStartup event. InternalStartup method will execute when a form loads.

public Form1(){InitializeComponent();//sync to the startup event where you can register for individual eventsformControl1.InternalStartup += new Microsoft.Office.InfoPath.FormControl.EventHandler<EventArgs>(InternalStartup);}

Next, implement your InternalStartup method. The function signature should look similar to:

void formControl1_InternalStartup(object sender, EventArgs e)

 

Step 2: Register to receive events

In your InternalStartup method add code to register for events. For form events this code looks like this.

void InternalStartup(object sender, EventArgs e){((FormControl)sender).EventManager.FormEvents.ViewSwitched += new ViewSwitchedEventHandler(OnSwitchView);((FormControl)sender).EventManager.FormEvents.Submit       += new SubmitEventHandler(OnSubmit);((FormControl)sender).EventManager.FormEvents.Sign         += new SignEventHandler(OnSign);((FormControl)sender).EventManager.FormEvents.Save         += new SaveEventHandler(OnSave);((FormControl)sender).EventManager.FormEvents.Merge        += new MergeEventHandler(OnMerge);}

For xml events you must provide the XPath of the node whose events you wish to respond to. Below is a sample of how to register to receive xml events.

 

void InternalStartup(object sender, EventArgs e){((FormControl)sender).EventManager.XmlEvents["/my:myFields/my:field1"].Changed    += new XmlChangedEventHandler(FieldChanged);((FormControl)sender).EventManager.XmlEvents["/my:myFields/my:field1"].Changing   += new XmlChangingEventHandler(FieldChanging);((FormControl)sender).EventManager.XmlEvents["/my:myFields/my:field1"].Validating += new XmlValidatingEventHandler(FieldValidating);}

To receive the click event for a button you must specify the control id of the button. Below is a sample of how to register to receive control events.

void InternalStartup(object sender, EventArgs e){((ButtonEvent)((FormControl)sender).EventManager.ControlEvents["CTRL2_5"]).Clicked += new ClickedEventHandler(ButtonClicked);}

 

Step 3: Implement methods to handle each event registered

The final step is to implement handlers for the events you have registered for.
The handlers for the events have the following method signatures.

Form Events

public delegate void ViewSwitchedEventHandler(object sender, Microsoft.Office.InfoPath.ViewSwitchedEventArgs e)public delegate void SubmitEventHandler(object sender, Microsoft.Office.InfoPath.SubmitEventArgs e)public delegate void SignEventHandler(object sender, Microsoft.Office.InfoPath.SignEventArgs e)public delegate void SaveEventHandler(object sender, Microsoft.Office.InfoPath.SaveEventArgs e)public delegate void MergeEventHandler(object sender, Microsoft.Office.InfoPath.MergeEventArgs e)

Xml Events

public delegate void XmlChangedEventHandler(object sender, Microsoft.Office.InfoPath.XmlEventArgs e)public delegate void XmlChangingEventHandler(object sender, Microsoft.Office.InfoPath.XmlChangingEventArgs e)public delegate void XmlValidatingEventHandler(object sender, Microsoft.Office.InfoPath.XmlValidatingEventArgs e)

Control Events

public delegate void ClickedEventHandler(object sender, Microsoft.Office.InfoPath.ClickedEventArgs e)

Example: changed event, view switched event, button clicked event.

void FieldChanged(object sender, XmlEventArgs e) {}void OnSwitchView(object sender, ViewSwitchedEventArgs e) {}void button1_Click(object sender, EventArgs e) {}

 

Things to know about handling events

  1. Events are handled first by the form’s business logic, then by others who register to handle events. Since events are handled asynchronously it is possible that the code in the business logic may cancel the event, and deny the host the opportunity to handle the event.
  2. In order to handle the submit, save and merging events, the designer of the form template must specify that the event is to be handled through the use of code. Without enabling this in the form template the event will not be handled in code.
  3. Buttons must specify that clicks be handled through code or the event will not be handled through code.
  4. The sign event will only take place when the form is fully trusted.
  5. The Loading and VersionUpgrade events are not available from third party hosted applications as these events occur before the form is loaded in the hosted application.

DeVere Dyett
Software Design Engineer in Test

Filed under: Writing Code

轉:http://blogs.msdn.com/infopath/archive/2007/02/07/using-the-events-manager-of-the-infopath-hosted-control.aspx

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.