天天看點

怎樣使用.NET RIA Services 建立 Silverlight Business Application(一)

HRApp 項目是.NET RIA Services MSDN  上的案例,一個使用Silverlight 3.0 + .Net RIA Service 建構的完整商業應用程式,

學習英文版的開發手冊總是比較費力的,是以順帶着簡略翻譯一下,也有部分自己心得體會,不足之處還望大家多多交流。

1 安裝

1. 解除安裝以前的版本

a. Microsoft Silverlight (任何Silverlight 3 RTW之前的版本)

b. Microsoft Silverlight SDK

c. Microsoft Silverlight Tools for Visual Studio 2008

2. 安裝.NET Framework 3.5 SP1 和Visual Studio 2008 SP1 (帶 SQL Express),參考:

http://msdn.microsoft.com/en-us/vstudio/cc533448.aspx

.

3. 順序安裝以下最新版本軟體:

a. Silverlight 3

b. Silverlight 3 SDK

c. Silverlight 3 Tools for Visual Studio 2008 SP1

4.  .NET RIA Services July Preview

2 建立工程

怎樣使用.NET RIA Services 建立 Silverlight Business Application(一)

選擇建立Silverlight Business Application ,會自動添加所需.net RIA引用,并生成一個Silverlight項目和一個web項目。其中的web項目已經內建了登入和注冊功能子產品。按F5運作檢視示範。預設的注冊功能需要確定本機已經安裝SQLSERVER 2005 EXPRESS ,第一次運作會提示更改配置檔案以啟用用戶端調試。

怎樣使用.NET RIA Services 建立 Silverlight Business Application(一)

2.1 建立界面

1. 打開用戶端工程中的MainTest.xaml檔案,找到下面的代碼

<TextBlock x:Name="ApplicationNameTextBlock" Style="{StaticResource ApplicationNameStyle}" Text="Application Name"/>

替換為

<TextBlock x:Name="ApplicationNameTextBlock" Style="{StaticResource ApplicationNameStyle}" Text="HR Application"/>

2. 選擇HRApp項目,右擊添加 New File.

3. 彈出對話框,選擇Page ,命名為EmployeeList.

怎樣使用.NET RIA Services 建立 Silverlight Business Application(一)

4. 打開 EmployeeList.xaml 在<Grid>标記下添加下面的代碼。

怎樣使用.NET RIA Services 建立 Silverlight Business Application(一)
怎樣使用.NET RIA Services 建立 Silverlight Business Application(一)

Code

<ScrollViewer BorderThickness="0" VerticalScrollBarVisibility="Auto" Padding="12,0,12,0" Margin="-12"> 

<StackPanel Margin="0,12,0,12" Orientation="Vertical" > 

<TextBlock Text="Employee List" Style="{StaticResource HeaderTextStyle}"/> 

</StackPanel> 

</ScrollViewer> 

5. 在項目面闆中将EmployeeList.xaml拖動到 Views 檔案夾中.

6. 打開MainPage.xaml 找到Home 連結按鈕并在旁邊再添加如下按鈕<!-- XAML -->

怎樣使用.NET RIA Services 建立 Silverlight Business Application(一)
怎樣使用.NET RIA Services 建立 Silverlight Business Application(一)

<HyperlinkButton x:Name="Link3" Style="{StaticResource LinkStyle}" NavigateUri="/EmployeeList" TargetName="ContentFrame" Content="employee list"/> 

<Rectangle x:Name="Divider2" Style="{StaticResource DividerStyle}"/> 

7. 運作後如下圖

怎樣使用.NET RIA Services 建立 Silverlight Business Application(一)

3 添加業務邏輯到 .NET RIA Services 工程

3.1 添加資料源

1. 将AdventureWorks_Data.mdf和AdventureWorks_Log.ldf添加到App_Data目錄下

2. 在web工程上右擊添加新項ADO.NET Entity Data Model

3. 命名為 AdventureWorks.edmx。

怎樣使用.NET RIA Services 建立 Silverlight Business Application(一)

4. 在實體模型向導中, 選擇generate the Model 後Next.

5. 選擇 AdventureWorks 資料庫 并設定實體集名稱為 AdventureWorks_DataEntities.

怎樣使用.NET RIA Services 建立 Silverlight Business Application(一)

6. 為了簡單起見,這裡我們從所有表中選擇Employee表作為實體模型. 設定命名空間為AdventureWorks_DataModel ,完成。

怎樣使用.NET RIA Services 建立 Silverlight Business Application(一)

7. 随後我們編譯一下整個項目。

3.2 添加Domain Service Objec t and Query for Data

1. 在web工程上右擊添加建立項(New Item)

2. 選擇Web分類下, Domain Service Class,命名為OrganizationService。

怎樣使用.NET RIA Services 建立 Silverlight Business Application(一)

3. 在彈出的對話框中選擇實體集中的Employee 表, 選中 Enable editing,Generate associated classes for metadata,Enable Client Access 三個CheckBox。

怎樣使用.NET RIA Services 建立 Silverlight Business Application(一)

4. 從剛建的服務類中,我們可以看到,因為選中了Enabled Edit,是以VS為我們加入了增,删,改等功能接口。

5. 為了程式示範效果,我們讓GetEmployee()傳回已排序的對象清單。

将下面代碼:

// C# 

public IQueryable<Employee> GetEmployee() 

    return this.Context.Employee; 

      return this.Context.Employee.OrderBy(e => e.EmployeeID); 

6. 按F6, 我們編譯一下整個項目。(因為隻有在每次編譯時,.net Service 才會将DomainContext和所有實體對象類自動生成到用戶端工程中)

7. 打開 EmployeeList.xaml. 從工具箱拖動一個DataGrid控件 到xaml. 并放在TextBlock下方。并向用戶端工程中手動添加

System.Windows.Controls.Data

引用,在xaml中添加

xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"

如果是在Blend 中則可以直接拖動,不用麻煩了。建構成如下這樣一個DataGrid。

<data:DataGrid x:Name="dataGrid1" MinHeight="100" IsReadOnly="True"></data:DataGrid> 

8. 打開EmployeeList.xaml.cs背景代碼,加入下面的命名空間引用。

using HRApp.Web; 

using System.Windows.Ria.Data; 

9. 當用戶端自動生成服務類代碼後,可以看到OrganizationContext 已經可以被智能識别了。那麼怎樣載入伺服器端的資料呢并綁定到DataGrid呢?隻需簡單的幾行代碼。

怎樣使用.NET RIA Services 建立 Silverlight Business Application(一)
怎樣使用.NET RIA Services 建立 Silverlight Business Application(一)

namespace HRApp 

    public partial class EmployeeList : Page 

OrganizationContext _OrganizationContext = new OrganizationContext(); 

public EmployeeList() 

InitializeComponent(); 

this.dataGrid1.ItemsSource = _OrganizationContext.Employees; 

_OrganizationContext.Load(_OrganizationContext.GetEmployeeQuery()); 

// Occurs when the user navigates to this page. 

protected override void OnNavigatedTo(NavigationEventArgs e) 

10. 按F5 運作程式,點選EmployeeList 按鈕,可以看到DataGrid和加載的資料.

怎樣使用.NET RIA Services 建立 Silverlight Business Application(一)
3.2.1 使用DomainDataSource 控件

1. 在ASP.net中我們看到過LinqDataSource,在這裡我們将添加一個新的控件DomainDataSource,其原理大緻是一樣的。

2. 首先在用戶端工程中需要添加程式集引用,System.Windows.Ria.Controls.dll

3. 如果使用VS2005請如果沒有請手動導入,在系統目錄 ‘%ProgramFiles%\Microsoft SDKs\RIA Services\v1.0\Libraries\Silverlight’ 下 .

怎樣使用.NET RIA Services 建立 Silverlight Business Application(一)

4. 如果使用Blend 3.0開發,隻需要拖動,在VS2008中要手動寫界面代碼。

5. 在XAML檔案中添加程式集引用

<!-- XAML -->

xmlns:ds="clr-namespace:HRApp.Web"

xmlns: riaControls ="clr-namespace:System.Windows.Controls;assembly=System.Windows.Ria.Controls"

6. 命名DomainDataSource 為employeeDataSource’ 并作如下設定

怎樣使用.NET RIA Services 建立 Silverlight Business Application(一)
怎樣使用.NET RIA Services 建立 Silverlight Business Application(一)

<riaControls:DomainDataSource x:Name="employeeDataSource" LoadSize="20" QueryName="GetSalariedEmployee" AutoLoad="True" > 

<riaControls:DomainDataSource.DomainContext> 

<ds:OrganizationContext/> 

</riaControls:DomainDataSource.DomainContext> 

</riaControls:DomainDataSource> 

7. 同時修改 DataGrid 代碼:

<data:DataGrid Height="Auto" MinHeight="100" IsReadOnly="True" ItemsSource="{Binding Data, ElementName=employeeDataSource}" 

x:Name="dataGrid1" /> 

8. 代開 EmployeeList.xaml.cs ,将代碼如下全部注釋。和LinqDataSource是不是很像?哈哈

怎樣使用.NET RIA Services 建立 Silverlight Business Application(一)
怎樣使用.NET RIA Services 建立 Silverlight Business Application(一)

//OrganizationContext _OrganizationContext = new OrganizationContext();

public EmployeeList()

{

    InitializeComponent();

    //this.dataGrid1.ItemsSource = _OrganizationContext.Employees;

    //_OrganizationContext.Load(_OrganizationContext.GetEmployeeQuery());

}

9. 可以運作程式了,看看資料是否顯示?It’s Great e, No Code!

3.2.2 給資料源添加排序,過濾,分頁功能

1. 向EmployeeList.xaml:中添加引用

xmlns:riaData="clr-namespace:System.Windows.Data;assembly=System.Windows.Ria.Controls" 

2. SortDescriptors 是 DomainDataSource提供的專門用于排序的接口屬性. 可以提供針對某個屬性列上的排序,PropertyPath是對應的對象屬性。

怎樣使用.NET RIA Services 建立 Silverlight Business Application(一)
怎樣使用.NET RIA Services 建立 Silverlight Business Application(一)

<riaControls:DomainDataSource x:Name="employeeDataSource" LoadSize="20" QueryName="GetEmployee" AutoLoad="True" > 

    <riaControls:DomainDataSource.DomainContext> 

        <ds:OrganizationContext/> 

    </riaControls:DomainDataSource.DomainContext> 

    <riaControls:DomainDataSource.SortDescriptors> 

        <riaData:SortDescriptor PropertyPath="VacationHours" Direction="Ascending" /> 

    </riaControls:DomainDataSource.SortDescriptors> 

3. 運作程式即可看到EmployeeList按VacationHours 升序.

4. 下面看怎樣使用過濾器.

怎樣使用.NET RIA Services 建立 Silverlight Business Application(一)
怎樣使用.NET RIA Services 建立 Silverlight Business Application(一)

                <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,-16,0,0">

                    <TextBlock VerticalAlignment="Center" Text="Min Vacation Hours Filter" />

                    <TextBox x:Name="vacationHoursText" Width="75" FontSize="11" Margin="4" Text="0"/>

                </StackPanel>

                <riaControls:DomainDataSource x:Name="employeeDataSource" LoadSize="20" QueryName="GetEmployeeQuery" AutoLoad="True" >

                    <riaControls:DomainDataSource.DomainContext>

                        <ds:OrganizationContext/>

                    </riaControls:DomainDataSource.DomainContext>

                    <!--排序器-->

                    <riaControls:DomainDataSource.SortDescriptors>

                        <riaData:SortDescriptor PropertyPath="VacationHours" Direction="Ascending" />

                    </riaControls:DomainDataSource.SortDescriptors>

                    <!--過濾器-->

                    <riaControls:DomainDataSource.FilterDescriptors>

                        <riaData:FilterDescriptorCollection>

                            <riaData:FilterDescriptor PropertyPath="VacationHours" Operator="IsGreaterThanOrEqualTo">

                                <riaData:ControlParameter ControlName="vacationHoursText"  PropertyName="Text" RefreshEventName="TextChanged" />

                            </riaData:FilterDescriptor>

                        </riaData:FilterDescriptorCollection>

                    </riaControls:DomainDataSource.FilterDescriptors>

                </riaControls:DomainDataSource>

                <!--DataGrid-->

                <data:DataGrid Height="Auto" MinHeight="100" IsReadOnly="True" ItemsSource="{Binding Data, ElementName=employeeDataSource}"  x:Name="dataGrid1" />

這裡藍色部分為過濾器提供了一個參數,這個參數值被綁定到一個TextBox控件的Text屬性值。

Operator為一個FilterOperator枚舉類型.RefreshEventName是使用Filter更新後的回調函數,這裡我們讓文本框在按下回車後就自動更新。

5. 運作程式,在“Min Vacation Hours Filter”旁的 TextBox中填入最小值,按回車。

怎樣使用.NET RIA Services 建立 Silverlight Business Application(一)

6. 下面我們給DataGrid 增加分頁功能,從工具箱拖動一個 DataPager 控件到EmployeeList.xaml.放在DataGrid的下面,

<data:DataPager PageSize="5" Source="{Binding Data, ElementName=employeeDataSource}" Margin="0,-1,0,0"></data:DataPager>

PageSize為頁面大小,這裡顯示5行,并綁定資料。

7. 運作程式點選 employee list 連結. 嘿嘿,效果不錯。

怎樣使用.NET RIA Services 建立 Silverlight Business Application(一)

時間有限,今天就到譯這裡,下一篇将加入資料綁定及綁定驗證。

繼續閱讀