更新: 2008 年 7 月
| 适用于 | 
|---|
| 本主题中的信息仅适用于指定的 Visual Studio Tools for Office 项目和 Microsoft Office 版本。 项目类型 
 Microsoft Office 版本 
 有关更多信息,请参见按应用程序和项目类型提供的功能。 | 
从 Visual Studio 2008 Service Pack 1 (SP1) 开始,您可以在应用程序级项目中将数据绑定到宿主控件。本演练演示如何在运行时将控件添加到 Microsoft Office Word 文档中、将控件绑定到从 MSDN Content Service 检索到的数据以及响应事件。
本演练演示以下任务:
- 在运行时将 RichTextContentControl 控件添加到文档中。 
- 将 RichTextContentControl 控件绑定到 Web 服务中的数据。 
- 响应 RichTextContentControl 控件的 Entering 事件。 
| .gif) 说明: | 
|---|
| 对于在以下说明中使用的某些 Visual Studio 用户界面元素,您的计算机可能会显示不同的名称或位置。这些元素取决于您使用的 Visual Studio 版本及设置。有关更多信息,请参见Visual Studio 设置。 | 
先决条件
您需要以下组件来完成本演练:
- Visual Studio Tools for Office(Visual Studio 2008 专业版 和 Visual Studio Team System 的可选组件)。 - 默认情况下,Visual Studio Tools for Office 随列出的 Visual Studio 版本一起安装。若要检查它是否已安装,请参见安装 Visual Studio Tools for Office。 
- Word 2007。 
创建新项目
第一步是创建 Word 外接程序项目。
创建新项目
- 使用 Visual Basic 或 C# 创建一个名为“MSDN Content Service”的“Word 2007 外接程序”项目。 - 有关更多信息,请参见如何:创建 Visual Studio Tools for Office 项目。 - Visual Studio 会打开 ThisAddIn.vb 或 ThisAddIn.cs 文件并将项目添加到“解决方案资源管理器”中。 
添加 Web 服务
对于本演练,请使用称为 MSDN Content Service 的 Web 服务。此 Web 服务以 XML 字符串或纯文本的形式返回指定 MSDN 文章中的信息。下一步演示如何在内容控件中显示返回的信息。
将 MSDN Content Service 添加到项目中
- 在“数据”菜单上单击“添加新数据源”。 
- 在“数据源配置向导”中单击“服务”,再单击“下一步”。 
- 在“地址”字段中键入下面的 URL: - http://services.msdn.microsoft.com/ContentServices/ContentService.asmx 
- 单击“转到”。 
- 在“命名空间”字段中键入 ContentService,再单击“确定”。 
- 在“添加引用向导”对话框中单击“完成”。 
在运行时添加内容控件并绑定到数据
在应用程序级项目中,在运行时添加和绑定控件。对于本演练,请将内容控件配置为当用户在内容控件内单击时检索 Web 服务中的数据。
添加内容控件并绑定到数据
- 在 ThisAddIn 类中,分别为 MSDN Content Service、内容控件和数据绑定声明变量。 - Private request As ContentService.getContentRequest Private proxy As ContentService.ContentServicePortTypeClient Private document As ContentService.requestedDocument() Private response As ContentService.getContentResponse Private appId As ContentService.appId Private WithEvents richTextContentControl _ As Microsoft.Office.Tools.Word.RichTextContentControl Private components As System.ComponentModel.Container Private primaryDocumentsBindingSource As _ System.Windows.Forms.BindingSource- private ContentService.getContentRequest request; private ContentService.ContentServicePortTypeClient proxy; private ContentService.requestedDocument[] document; private ContentService.getContentResponse response; private ContentService.appId appId; private Microsoft.Office.Tools.Word.RichTextContentControl richTextContentControl; private System.ComponentModel.Container components; private System.Windows.Forms.BindingSource primaryDocumentsBindingSource;
- 将下面的方法添加到 ThisAddIn 类中。此方法会在活动文档的开始处创建一个内容控件。 - Private Sub AddRichTextControlAtRange() Dim currentDocument As Word.Document = Me.Application.ActiveDocument currentDocument.Paragraphs(1).Range.InsertParagraphBefore() Dim extendedDocument As Document = currentDocument.GetVstoObject() richTextContentControl = _ extendedDocument.Controls.AddRichTextContentControl _ (currentDocument.Paragraphs(1).Range, "richTextControl2") richTextContentControl.PlaceholderText = _ "Click here to download MSDN Library information about content controls." End Sub- private void AddRichTextControlAtRange() { Word.Document currentDocument = this.Application.ActiveDocument; currentDocument.Paragraphs[1].Range.InsertParagraphBefore(); Document extendedDocument = currentDocument.GetVstoObject(); richTextContentControl = extendedDocument.Controls.AddRichTextContentControl( currentDocument.Paragraphs[1].Range, "richTextContentControl"); richTextContentControl.PlaceholderText = "Click here to download MSDN Library information about content controls."; }
- 将下面的方法添加到 ThisAddIn 类中。此方法会初始化需要创建的对象并向 Web 服务发送一个请求。 - Private Sub InitializeServiceObjects() request = New ContentService.getContentRequest() proxy = New ContentService.ContentServicePortTypeClient() document = New ContentService.requestedDocument(0) {} response = New ContentService.getContentResponse() appId = New ContentService.appId() components = New System.ComponentModel.Container() primaryDocumentsBindingSource = _ New System.Windows.Forms.BindingSource(components) End Sub- private void InitializeServiceObjects() { request = new ContentService.getContentRequest(); proxy = new ContentService.ContentServicePortTypeClient(); document = new ContentService.requestedDocument[1]; response = new ContentService.getContentResponse(); appId = new ContentService.appId(); components = new System.ComponentModel.Container(); primaryDocumentsBindingSource = new System.Windows.Forms.BindingSource(this.components); }
- 创建一个事件处理程序,当用户在内容控件内单击时检索有关内容控件的 MSDN Library 文档,并将数据绑定到内容控件。 - Private Sub richTextContentControl_Entering _ (ByVal sender As Object, ByVal e As ContentControlEnteringEventArgs) _ Handles richTextContentControl.Entering document(0) = New ContentService.requestedDocument() With document(0) .type = ContentService.documentTypes.primary .selector = "Mtps.Xhtml" End With With request .contentIdentifier = "ed59e522-dd6e-4c82-8d49-f5dbcfcc950d" .locale = "en-us" .version = "VS.90" .requestedDocuments = document End With response = proxy.GetContent(appId, request) primaryDocumentsBindingSource.DataSource = _ response.primaryDocuments(0).Any.InnerText richTextContentControl.DataBindings.Add( _ "Text", _ primaryDocumentsBindingSource.DataSource, _ "", _ True, _ System.Windows.Forms.DataSourceUpdateMode.OnValidation) End Sub- void richTextContentControl_Entering(object sender, ContentControlEnteringEventArgs e) { document[0] = new ContentService.requestedDocument(); document[0].type = ContentService.documentTypes.primary; document[0].selector = "Mtps.Xhtml"; request.contentIdentifier = "ed59e522-dd6e-4c82-8d49-f5dbcfcc950d"; request.locale = "en-us"; request.version = "VS.90"; request.requestedDocuments = document; response = proxy.GetContent(appId, request); primaryDocumentsBindingSource.DataSource = response.primaryDocuments[0].Any.InnerText; richTextContentControl.DataBindings.Add( "Text", primaryDocumentsBindingSource.DataSource, "", true, System.Windows.Forms.DataSourceUpdateMode.OnValidation); }
- 从 ThisAddIn_Startup 方法调用 AddRichTextControlAtRange 和 InitializeServiceObjects 方法。对于 C# 程序员,添加一个事件处理程序。 - Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup AddRichTextControlAtRange() InitializeServiceObjects() End Sub- private void ThisAddIn_Startup(object sender, System.EventArgs e) { AddRichTextControlAtRange(); InitializeServiceObjects(); this.richTextContentControl.Entering += new EventHandler<ContentControlEnteringEventArgs> (richTextContentControl_Entering); }
测试外接程序
打开 Word 后,RichTextContentControl 控件会随即显示。当在该控件内单击时,该控件中的文本会发生变化。
测试外接程序
- 按“F5”。 
- 在内容控件内单击。 - 随即从 MSDN Content Service 下载信息并将信息显示在内容控件中。 
请参见
概念
修订记录
| 日期 | 修订历史记录 | 原因 | 
|---|---|---|
| 2008 年 7 月 | 新增主题。 | SP1 功能更改。 |