我的最终目标是将文件从一个Lync客户端传输到另一个。我有以下代码。
首先,我注册了2个事件 1。
((Modality)_conversation.Modalities[ModalityTypes.ContentSharing]).ModalityStateChanged += Modality_ModalityStateChanged;
2
((ContentSharingModality)_conversation.Modalities[ModalityTypes.ContentSharing]).ContentAdded += _sharingModality_ContentAdded;
这些事件的代码是
void _sharingModality_ContentAdded(object sender, ContentCollectionChangedEventArgs e)
{
MessageBox.Show("content added\n"+e.Item);
}
void Modality_ModalityStateChanged(object sender, ModalityStateChangedEventArgs e)
{
if (e.NewState == ModalityState.Connected)
{
textBox1.Text += "\nconnected";
send_file();
}
if (e.NewState == ModalityState.Connecting)
{
textBox1.Text += "\nconnecting";
}
}
然后我有一个方法,在名为" abc.txt"的隔离存储中创建一个文件。 接下来是一个连接内容共享模式的代码。
private void button4_Click(object sender, RoutedEventArgs e)
{
if (_conversation.State == ConversationState.Active)
{
((Modality)_conversation.Modalities[ModalityTypes.ContentSharing])
.BeginConnect((ar) =>{((Modality)_conversation.Modalities[ModalityTypes.ContentSharing]).EndConnect(ar); }
, null);
else { MessageBox.Show("conversation not active"); }
}
此后有' send_file'实际上传文件的方法。 (此方法id先前在模态状态更改为“已连接”时调用,但是(我认为)会话更改为multiparty,方法在“canInvoke”语句中返回false。所以我再次调用它这次成功了。如下所示
void send_file()
{
if (((ContentSharingModality)_conversation.Modalities[ModalityTypes.ContentSharing]).State == ModalityState.Connected)
{
try
{
if (((ContentSharingModality)_conversation.Modalities[ModalityTypes.ContentSharing]).CanInvoke(ModalityAction.CreateShareableNativeFileOnlyContent))
{
ContentSharingModality contentSharingModality = (ContentSharingModality)_conversation.Modalities[ModalityTypes.ContentSharing];
contentSharingModality.BeginCreateContentFromFile(ShareableContentType.NativeFile, "samplefile.txt", fileNameFromIsolatedStorage, true,
(ar) =>
{
ShareableContent sContent = contentSharingModality.EndCreateContentFromFile(ar);
//_NativeFileNameAndPath = string.Empty;
sContent.Upload();
}
, null);
MessageBox.Show("upload done");
}
else { MessageBox.Show("u cannot invoke"); }
}
catch (Exception e1) { MessageBox.Show(e1.Message); }
}
else { MessageBox.Show("modality inactive"); }
}
最后,这就是我想做的事情。发件人和发件人都会使用相同的代码。接收机。我是lync开发的新手,对于出了什么问题非常困惑。请帮忙。谢谢!