0

我的最终目标是将文件从一个 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”方法。(当模态状态更改为“已连接”但(我认为)对话更改为多方并且方法在“canInvoke”语句中返回 false 时,此方法 id 以前调用过。所以我再次调用它,这次它成功了。如下所示

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 开发的新手,对出了什么问题非常困惑。请帮忙。谢谢!

4

0 回答 0