轉帖|其它|編輯:郝浩|2010-12-09 11:35:38.000|閱讀 985 次
概述:在Windows或者ASP.NET Web應用程序中,我們經常可以看到在Grid控件上通過Load-on-demand的方式來提高系統性能,提升用戶體驗。所謂Load-on-demand就是在最初表格數據加載時只加載當前表格中用戶可以看到的行數,當用戶向下滾動或拖拽縱向滾動條時,再將需要顯示的數據通過某種方式動態加載進來。 本文主要介紹Silverlight DataGrid使用WCF RIA Service實現Load-on-demand的數據加載,希望對大家有幫助。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
在Windows或者ASP.NET Web應用程序中,我們經常可以看到在Grid控件上通過Load-on-demand的方式來提高系統性能,提升用戶體驗。
所謂Load-on-demand就是在最初表格數據加載時只加載當前表格中用戶可以看到的行數,當用戶向下滾動或拖拽縱向滾動條時,再將需要顯示的數據通過某種方式動態加載進來。
那么對于Silverlight,我們可以使用DataGrid通過WCF RIA Service來實現這個功能。
1. WCF RIA Service
我們將會使用WCF Service來提供數據,并且將這個WCF Service host到ASP.Net應用程序中。
- 定義數據對象
[DataContract]
public class Employee
{
[DataMember]
public int ID { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Department { get; set; }
[DataMember]
public double Salary { get; set; }
}
使用DataContract和DataMember來標識數據對象以及對象屬性,這樣就可以通過WCF Service來傳遞這個數據結構了,注意需要添加System.Runtime.Serialization.dll。
添加Silverlight enabled WCF Service
在WebApplciation工程中添加一個新的Item,選取"Silverlight enabled WCF Service",命名為"EmployeeService.svn".
在PepoleService.svn.cs中添加如下代碼:
[OperationContract]
public List<Employee> GetEmployeeData(int startRow, int endRow)
{
List<Employee> employees = new List<Employee>();
for (int i = startRow; i < endRow; i++)
{
employees.Add(new Employee()
{
ID = i,
Name = string.Format("Name {0}", i),
Department = string.Format("Department {0}", i),
Salary = i * 100.0
});
}
return employees;
}
注意在上面一步添加完WCF Service后,會在Web.config文件中添加關于Service的配置信息:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="Silverlight.Web.EmployeeServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<services>
<service behaviorConfiguration="Silverlight.Web.EmployeeServiceBehavior" name="Silverlight.Web.EmployeeService">
<endpoint address="" binding="basicHttpBinding" contract="Silverlight.Web.EmployeeService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
2. Load-on-demand數據加載
在Silverlight工程中添加Service引用
如下圖通過給Silverlight工程添加Service reference來操作WCF Service:
從WCFService中獲取數據
將WCF Service引入后,IDE會自動生成EmployeeServiceClient類,通過這個代理我們就可以使用Service上的方法了。
通過如下代碼可以從WCF Service獲得Employee數據:
public partial class Page : UserControl
{
private ObservableCollection<Employee> _employees;
private void GetData(int startRow, int endRow)
{
EmployeeServiceClient proxy = new EmployeeServiceClient();
proxy.GetEmployeeDataCompleted += new EventHandler<GetEmployeeDataCompletedEventArgs>(proxy_GetDataCompleted);
proxy.GetEmployeeDataAsync(startRow, endRow);
}
private void proxy_GetDataCompleted(object sender, GetEmployeeDataCompletedEventArgs e)
{
foreach (Employee employee in e.Result)
{
this._employees.Add(employee);
}
}
}
在DataGrid上實現數據的Load-on-demand
Silverlight DataGrid提供了一個事件:LoadingRow,該事件會在某一個Row第一次被顯示的時候被觸發。通過這個事件我們就可以實現數據的按需加載,在這個事件中我們可以拿到該Row的RowIndex,如果發現當前將要顯示的Row已經接近末尾(當前定義為距離末尾小于5),那么就需要向服務器端請求數據。
示例代碼:
private void Page_Loaded(object sender, RoutedEventArgs e)
{
this._startRowIndex = 0;
this._employees = new ObservableCollection<Employee>();
this.peopleDataGrid.ItemsSource = _employees;
GetData(this._startRowIndex, this._pageSize);
}
private void peopleDataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
if (this._isLoading || this._employees.Count < _pageSize)
{
return;
}
if (this._employees.Count - 5 < e.Row.GetIndex())
{
this.GetData(this._startRowIndex, this._startRowIndex + this._pageSize);
}
}
運行程序,拖動ScrollBar到底部,你會發現DataGrid會自動加載數據。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:博客轉載