yinghuoplan / 536000-

新加文档信息
Artistic License 2.0
0 stars 0 forks source link

创建包含 WCF RIA Services 类库的 Silverlight 解决方案 #3

Open neemo26 opened 10 years ago

neemo26 commented 10 years ago

创建 RIA Services 类库

WCF RIA Services 4(共 4)对本文的评价是有帮助 - 评价此主题 在本演练中,您将学习如何创建一个 Silverlight 应用程序,该程序在 WCF RIA Services 类库中包含可重用的组件。 使用 WCF RIA Services 类库可以创建可重用的中间层和表示层逻辑。但是,使用 RIA Services 类库比创建 RIA Services 解决方案复杂。 为了简化示例,本演练显示 Silverlight 应用程序所在的解决方案中的类库。该类库可以存在于单独的解决方案中。 有关 RIA Services 类库的一些更多背景,请参见创建 RIA Services 解决方案主题的相关章节。

neemo26 commented 10 years ago

通过依次选择“文件”、“新建”和“项目”,在 Visual Studio 2010 中创建一个新的 RIA Services 项目。 将显示“新建项目”对话框。 从“已安装的模板”的“Silverlight”组中选择“Silverlight 应用程序”模板,然后将新项目命名为 ExampleSilverlightApp。 在“新建 Silverlight 应用程序”对话框中,不为应用程序选中“启用 WCF RIA Services”框。 Silverlight 应用程序不需要 Silverlight 项目和服务器项目之间的 RIA Services 链接,因为在类库中 RIA Services 链接在这两个项目之间存在。 在“解决方案资源管理器”中,右击该解决方案,选择“添加”,然后选择“新建项目”。 随即出现“添加新项目”对话框。 在“Silverlight”类别“已安装的模板”中,选择“WCF RIA Services 类库”模板并将它命名为 AdventureWorksClassLibrary。 单击“确定”。

neemo26 commented 10 years ago

右击 ExampleSilverlightApp.Web 项目,然后选择“添加引用”。 将显示“添加引用”对话框。 在“项目”选项卡上,选择“AdventureWorksClassLibrary.Web”项目,然后单击“确定”。 右击 ExampleSilverlightApp 项目,然后选择“添加引用”。 在“项目”选项卡上,选择“AdventureWorksClassLibrary”项目,然后单击“确定”。 创建中间层库 在 AdventureWorksClassLibrary.Web 项目中,添加名为 AdventureWorksModel.edmx 的“ADO.NET 实体数据模型”。有关如何执行此操作的步骤,请参见演练:创建 RIA Services 解决方案。 在“实体数据模型”向导中,在实体模型中包含“产品”表。 生成 (Ctrl+Shift+B) 该解决方案。

neemo26 commented 10 years ago

右击 AdventureWorksClassLibrary.Web 项目,选择“添加”,然后选择“新建项”。 选择“域服务类”模板并将它命名为 ProductsDomainService。 单击“添加”。 将显示“添加新的域服务类”对话框。 从要通过域服务公开的可用数据模型中选择“产品”,然后单击“确定”。 生成 (Ctrl+Shift+B) 该解决方案。 在“解决方案资源管理器”中,在每个项目中选择“显示所有文件”。 请注意 Generated_Code 文件夹只存在于 AdventureWorksClassLibrary 项目中。尽管没有为 ExampleSilverlightApp 项目生成代码,但是您仍可以使用从 AdventureWorksClassLibrary 项目中生成的代码,因为在 ExampleSilverlightApp 和 AdventureWorksClassLibrary 项目之间存在一个项目引用。

neemo26 commented 10 years ago

在 Silverlight 项目中使用生成的代码 右击 ExampleSilverlightApp 项目,然后选择“添加引用”。 添加对 System.ServiceModel.DomainServices.Client 程序集的引用。 若要查找该程序集,请选择“.NET”选项卡。 在 ExampleSilverlightApp 项目中,打开 MainPage.xaml。 从“工具箱”中,将 DataGrid 控件拖到 Grid 元素内。 将添加 XML 命名空间和对“数据”程序集的引用。

neemo26 commented 10 years ago

命名 DataGridProductsGrid,如下面的 XAML 中所示: CS <UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
x:Class="ExampleSilverlightApp.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://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>

neemo26 commented 10 years ago

打开 MainPage.xaml 的代码隐藏文件。 添加以下代码以检索产品: VB 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

neemo26 commented 10 years ago

CS 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;
    }
}

}

neemo26 commented 10 years ago

打开 AdventureWorksClassLibrary.Web 项目中的 App.Config 文件,并逐个复制 元素以及它们包含的元素。将每个元素逐个粘贴到 ExampleSilverlightApp.Web 项目的 Web.config 文件。Web.config 文件类似于以下示例,但是您的文件必须提供适合您环境的相关连接信息: CS

neemo26 commented 10 years ago
neemo26 commented 10 years ago