逐步解說:在應用程式層級專案中繫結至服務資料
您可以將資料繫結至應用程式層級專案中的控制項。本逐步解說示範如何在執行階段,將控制項加入至 Microsoft Office Word 文件、將控制項繫結至擷取自 MSDN 內容服務的資料,以及回應事件。
本主題中的資訊適用於應用程式層級專案 Word 上的套用: 。如需詳細資訊,請參閱依 Office 應用程式和專案類型提供的功能。
這個逐步解說將說明下列工作:
在執行階段將 RichTextContentControl 控制項加入至文件。
將 RichTextContentControl 控制項繫結至 Web 服務的資料。
回應 RichTextContentControl 控制項的 Entering 事件。
![]() |
---|
您的電腦可能會在下列說明中,以不同名稱或位置顯示某些 Visual Studio 使用者介面項目。您所擁有的 Visual Studio 版本以及使用的設定會決定這些項目。如需詳細資訊,請參閱 Visual Studio 設定。 |
必要條件
您需要下列元件才能完成此逐步解說:
-
包含 Microsoft Office Developer 工具的 Visual Studio 2012 版本。如需詳細資訊,請參閱[設定電腦以開發 Office 方案](bb398242\(v=vs.110\).md)。
- Word 2013 或 Word 2010。
建立新專案
首先,必須建立 Word 增益集專案。
若要建立新的專案
使用 Visual Basic 或 C#,建立名為「MTPS 內容服務」的 Word 增益集專案。
如需詳細資訊,請參閱HOW TO:在 Visual Studio 中建立 Office 專案。
Visual Studio 隨即開啟 ThisAddIn.vb 或 ThisAddIn.cs 檔案,並將專案加入至 [方案總管]。
加入 Web 服務
在本逐步解說中,將使用名為「MTPS 內容服務」的 Web 服務。這個 Web 服務會以 XML 字串或純文字的形式,傳回指定之 MSDN 文件的資訊。後面的步驟將會說明如何顯示透過內容控制項所傳回的資訊。
若要將 MTPS 內容服務加入至專案
在 [資料] 功能表上,請按一下 [加入新資料來源]。
在 [資料來源組態精靈] 中,按一下 [服務],然後按 [下一步]。
在 [位址] 欄位中輸入下列 URL:
http://services.msdn.microsoft.com/ContentServices/ContentService.asmx
按一下 [移至]。
在 [命名空間] 欄位中,輸入 ContentService,然後按一下 [確定]。
在 [加入參考精靈] 對話方塊中,按一下 [完成]。
在執行階段加入內容控制項並繫結至資料
透過應用程式層級專案,您可以在執行階段加入並繫結控制項。在本逐步解說中,請將內容控制項設定為每當使用者在控制項內按一下時,就擷取 Web 服務中的資料。
若要加入內容控制項並繫結至資料
在 ThisAddIn 類別中,宣告 MTPS 內容服務、內容控制項及資料繫結的變數。
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 = Globals.Factory.GetVstoObject(currentDocument) 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 = Globals.Factory.GetVstoObject(currentDocument); 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 += richTextContentControl_Entering; }
測試增益集
當您開啟 Word 時,RichTextContentControl 控制項隨即出現。當您在控制項內按一下時,其中的文字會改變。
若要測試增益集
請按 F5。
在內容控制項內按一下。
資訊隨即從 MTPS 內容服務下載,並顯示在內容控制項中。