Freigeben über


Handling Lync Events (Lync 2010 SDK)

Events can be raised by several classes in Microsoft Lync 2010 as the state of the class instance changes. A state change can be the result of an action by the self-user or it can be the result of an action by a remote contact. When a state change involves the addition of an item to a collection, you should use an event handler to register for events on the new item added with the event. When an item is removed, use the event handler to remove an event registration on the removed collection item.

For example, when a group is added to the Groups collection, the GroupAdded event is raised. You should register for contact collection related events such as ContactAdded event and ContactRemoved that are raised when the contact collection encapsulated by the Group changes.

Events are also raised when the state of an object such as a modality changes. For example, the Microsoft.Lync.Model.Conversation.AudioVideo.AVModality goes from a Microsoft.Lync.Model.Conversation.ModalityState.Connecting to .Connected as a video conversation is started. You use the state change event to update your UI by starting a VideoWindow to show a conversation video stream.

Registering for Events

The following example registers the application for the ContactAdded event of the group, designating group_ContactAdded as the method to be triggered by the event. Note that the syntax below is auto-completed by Visual Studio.

thisGroup.ContactAdded +=new System.EventHandler<Object,GroupMemberChangedEventArgs>(group_ContactAdded);

You can create the same event registration using a shorter form of the previous syntax. Note that when you do not use the auto-complete feature to write the registering statement, the event handler method is not auto-generated.

thisGroup.ContactAdded += group_ContactAdded;

Handling Events

Events are raised when the state of the object is changed. For example, the ContactAdded event is raised on a group when the state of the group is updated with the addition of a new contact. If your application registered for the ContactAdded event on a group, as described earlier, and a contact is added to the group, the event handler defined in the registration is called. In the following example, the group_ContactAdded event handler updates the user interface and subscribes to presence items and registers for events on the new contact.

        void group_ContactAdded(object source, GroupMemberChangedEventArgs data)
        {
            // Update application UI with event notification.
            System.Windows.Forms.MessageBox.Show(
                "New Contact in Group: " 
                + ((Group)source).Name 
                + " : " 
                + data.Contact.GetContactInformation(ContactInformationType.DisplayName).ToString());
                // Listen for contact events.
                data.Contact.SettingChanged += contact_SettingChanged;
                data.Contact.ContactInformationChanged += contact_ContactInformationChanged;
                data.Contact.UriChanged += contact_UriChanged;
        }

Removing Event Registration

Remove your registrations for events when the object is disposed of. Event registration removal ensures that memory resources are not wasted. You remove a registration with one line of code.

For example, the following code is the event handler called when a contact is removed from a group. This code removes the contact from the client’s subscription and cancels the registration for each event for the contact.

        void group_ContactRemoved(object source, GroupMemberChangedEventArgs data)
        {
            // Update application UI with event notification.
            System.Windows.Forms.MessageBox.Show(
                "Contact was removed from Group: " 
                + ((Group)source).Name);
                data.Contact.ContactPropertyChanged -= contact_SettingChanged;
                data.Contact.PresenceItemsChanged -= contact_ContactInformationChanged;
                data.Contact.UriChanged -= contact_UriChanged;

        }

See Also

Concepts

Handle Events for ContactManager (Lync 2010 SDK)

Handle Events for a Group (Lync 2010 SDK)

Handle Events for a Contact (Lync 2010 SDK)