逐步解說:建立 RIA Services 類別庫
在本逐步解說中,您將了解如何建立包含 WCF RIA Services 類別庫中可重複使用之元件的 Silverlight 應用程式。
WCF RIA Services 類別庫可讓您建立可重複使用的中介層和展示層邏輯。不過,RIA Services 類別庫的使用比 RIA Services 方案的建立還要複雜。
為簡化範例,本逐步解說所述的類別庫位於與 Silverlight 應用程式相同的方案中。類別庫可以存在於不同的方案中。
如需 RIA Services 類別庫的詳細背景,請參閱建立 RIA Services 方案主題的相關小節。
注意: |
---|
如果您不需要建立可重複使用的元件,或想要查看有關建立 RIA Services 方案的更入門逐步解說,請參閱逐步解說:建立 RIA Services 方案。 |
必要條件
除了 WCF RIA Services 和 WCF RIA Services 工具組之外,在 WCF RIA Services 文件中呈現的這個逐步解說和其他逐步解說還需要正確安裝並設定數個必要程式 (例如 Visual Studio 2010 和 Silverlight 開發人員執行階段與 SDK)。要執行逐步解說還需要安裝並設定 SQL Server 2008 R2 Express with Advanced Services,以及安裝 AdventureWorks OLTP 和 LT 資料庫。
在 WCF RIA Services 的必要條件節點中的主題也提供符合這些必要條件的詳細指示。請先按照該處提供的指示進行,然後再進行本逐步解說,以確保您在進行本 RIA Services 逐步解說時不會發生問題。
若要建立包含 WCF RIA Services 類別庫的 Silverlight 方案
在 Visual Studio 2010 中建立新的 RIA Services 專案,方式是依序選取 [檔案]、[新增] 和 [專案]。
[新增專案] 對話方塊隨即出現。
從 [已安裝的範本] 的 [Silverlight] 群組選取 [Silverlight 應用程式] 範本,並將新專案命名為 ExampleSilverlightApp。
在 [新 Silverlight 應用程式] 對話方塊中,不要選取應用程式的 [啟用 WCF RIA Services] 方塊。
Silverlight 應用程式在 Silverlight 專案與伺服器專案之間並不需要 RIA Services 連結,因為 RIA Services 連結將存在於類別庫中的專案之間。
在 [方案總管] 中,以滑鼠右鍵按一下方案,然後選取 [加入],再選取 [新增專案]。
[加入新的專案] 對話方塊隨即出現。
從 [Silverlight] 分類 [已安裝的範本] 選取 [WCF RIA Services 類別庫] 範本,並將它命名為 AdventureWorksClassLibrary。
按一下 [確定]。
您的方案現在包含四個專案,如下圖所示。
以滑鼠右鍵按一下 ExampleSilverlightApp.Web 專案,然後選取 [加入參考]。
[加入參考] 對話方塊隨即出現。
選取 [專案] 索引標籤上的 [AdventureWorksClassLibrary.Web] 專案,然後按一下 [確定]。
以滑鼠右鍵按一下 ExampleSilverlightApp 專案,然後選取 [加入參考]。
選取 [專案] 索引標籤上的 [AdventureWorksClassLibrary] 專案,然後按一下 [確定]。
若要建立中介層程式庫
在 AdventureWorksClassLibrary.Web 專案中加入 [ADO.NET 實體資料模型],並命名為 AdventureWorksModel.edmx。如需如何執行此工作的詳細資訊,請參閱逐步解說:建立 RIA Services 方案。
在 [實體資料模型] 精靈中,將 [Product] 資料表包含在實體模型中。
建置 (Ctrl+Shift+B) 方案。
以滑鼠右鍵按一下 AdventureWorksClassLibrary.Web 專案,然後選取 [加入],再選取 [新增項目]。
選取 [DomainService 類別] 範本,並將它命名為 ProductsDomainService。
按一下 [加入]。
[加入新的 DomainService 類別] 對話方塊隨即出現。
從可用的資料模型選取 [Product] 以透過網域服務公開,然後按一下 [確定]。
建置 (Ctrl+Shift+B) 方案。
在 [方案總管] 中,選取每個專案中的 [顯示所有檔案]。
請注意,[Generated_Code] 資料夾只存在於 AdventureWorksClassLibrary 專案中。雖然沒有 ExampleSilverlightApp 專案產生的程式碼,但還是可以使用從 AdventureWorksClassLibrary 專案產生的程式碼,因為 ExampleSilverlightApp 與 AdventureWorksClassLibrary 專案之間存在專案參考。
若要在 Silverlight 專案中使用產生的程式碼
以滑鼠右鍵按一下 ExampleSilverlightApp 專案,然後選取 [加入參考]。
加入 System.ServiceModel.DomainServices.Client 組件的參考。
若要尋找組件,請選取 [.NET] 索引標籤。
開啟 ExampleSilverlightApp 專案中的 MainPage.xaml 檔案。
將 DataGrid 控制項從 [工具箱] 拖曳至 Grid 項目中。
XML 命名空間和資料組件的參考隨即加入。
將 DataGrid 命名為
ProductsGrid
,如下列 XAML 所示。<UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" x:Class="ExampleSilverlightApp.MainPage" xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="https://schemas.microsoft.com/expression/blend/2008" xmlns:mc="https://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"> <Grid x:Name="LayoutRoot" Background="White"> <data:DataGrid Name="ProductsGrid"></data:DataGrid> </Grid> </UserControl>
開啟 MainPage.xaml 的程式碼後置檔案。
加入下列程式碼以擷取產品。
Imports System.ServiceModel.DomainServices.Client Imports AdventureWorksClassLibrary.Web Partial Public Class MainPage Inherits UserControl Private _productContext As New ProductsDomainContext Public Sub New() InitializeComponent() Dim loadOp = Me._productContext.Load(Me._productContext.GetProductsQuery()) ProductsGrid.ItemsSource = loadOp.Entities End Sub End Class
using System.ServiceModel.DomainServices.Client; using AdventureWorksClassLibrary.Web; namespace ExampleSilverlightApp { public partial class MainPage : UserControl { private ProductsDomainContext _productContext = new ProductsDomainContext(); public MainPage() { InitializeComponent(); LoadOperation<Product> loadOp = this._productContext.Load(this._productContext.GetProductsQuery()); ProductsGrid.ItemsSource = loadOp.Entities; } } }
開啟 AdventureWorksClassLibrary.Web 專案中的 App.Config 檔案,然後個別複製
<connectionStrings>
、<system.serviceModel>
和<httpModules>
項目及其所包含的項目。分別將每個項目貼到 ExampleSilverlightApp.Web 專案的 Web.config 檔案。Web.config 檔案看起來像下面的範例,但是您的檔案必須提供您的環境之相關連接資訊。<configuration> <connectionStrings> <add name="AdventureWorksLT2008Entities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="Data Source=example;Initial Catalog=AdventureWorksLT2008;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" /> </connectionStrings> <system.serviceModel> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> </system.serviceModel> <system.web> <compilation debug="true" targetFramework="4.0" /> <httpModules> <add name="DomainServiceModule" type="System.ServiceModel.DomainServices.Hosting.DomainServiceHttpModule, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> </httpModules> </system.web> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer> </configuration>
執行 (F5) 應用程式。