Reputation:
I use the Lync SDK 2013 and try to check if a new conversation is incoming or outgoing. I don't want to check for audio/video calls only, I want to check in on each modality type.
private void Conversation_Added(object sender, ConversationManagerEventArgs e)
{
Conversation conversation = e.Conversation;
IDictionary<ModalityTypes, Modality> modalities = conversation.Modalities;
bool conversationIsIncoming = modalities.Any(modality => modality.Value.State == ModalityState.Notified);
}
When the event gets triggered and it comes to the Any
method I get this error
NullReferenceException object reference not set to an instance of an object. System.Collections.Generic.KeyValuePair.Value.get returned null.
So obviously I have to use a null check here but maybe the whole code may be wrong? How can I check if the conversation is incoming or outgoing?
Upvotes: 0
Views: 274
Reputation: 14138
Your idea is basically correct but when you check for the notified state is incorrect.
You need to hook the ModalityStateChanged event, and if you only want to know about audio/video "Calls" then you also only need to hook for conversations that have AudioVideo modality type.
e.g.
private void ConversationManager_ConversationAdded(object sender, ConversationManagerEventArgs e)
{
if (e.Conversation.Modalities.TryGetValue(ModalityTypes.AudioVideo, out var avModality))
{
avModality.ModalityStateChanged += AvModalityOnStateChanged;
}
}
private void AvModalityOnStateChanged(object sender, ModalityStateChangedEventArgs e)
{
if (e.NewState == ModalityState.Notified)
{
bool conversationIsIncoming = true;
}
}
Don't forget to unhook from the ModalityStateChanged when you don't need to know the state change any longer.
Upvotes: 0