微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

silverlight 服务器端分页实现

silverlight 服务器端分页实现       

        分类:            silverlight 538人阅读 评论(0) 收藏 举报

silverlight 分页服务端分页就是在服务里面写分页方法,silverlight 调用的时候传参数过来就行了。先看服务代码

我这边使用的是wcf服务!类图如下:

服务写了两个方法,PageCount这个方法获取数据总条数,PageFilter用于分页。该方法接受三个参数,PageSize、PageIndex、Filter,当然分页的前两个参数是必不可少的,其他的参数可以根据实际情况来定义;如果你要根据用户名来过滤的话,加个string UserName就行了;废话不多说。直接看该方法代码

  1. using System
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Runtime.Serialization; 
  5. using System.ServiceModel; 
  6. using System.Text; 
  7.  
  8. namespace Web 
  9.     // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“WebPageService”。 
  10.     public class WebPageService : IWebPageService 
  11.     { 
  12.  
  13.         public List<PageEntity> PageFilter(int? PageSize,int PageIndex,string filter) 
  14.         { 
  15.             List<PageEntity> sources = PageEntity.CreatePageSources(); 
  16.             return sources.Skip((PageSize * (PageIndex - 1)).Value).Take(PageSize.Value).ToList(); 
  17.         } 
  18.  
  19.  
  20.         public int PageCount(string filter) 
  21.         { 
  22.             return 6; 
  23.         } 
  24.     } 
  25.  
  26.     public class PageEntity 
  27.     { 
  28.         public static List<PageEntity> CreatePageSources() 
  29.         { 
  30.             List<PageEntity> entitys = new List<PageEntity>(); 
  31.             entitys.Add(new PageEntity() 
  32.             { 
  33.                 UserName="aa"
  34.                 UserSex="男" 
  35.             }); 
  36.             entitys.Add(new PageEntity() 
  37.             { 
  38.                 UserName = "aaa"
  39.                 UserSex = "女" 
  40.             }); 
  41.             entitys.Add(new PageEntity() 
  42.             { 
  43.                 UserName = "aaa1"
  44.                 UserSex = "女" 
  45.             }); 
  46.             entitys.Add(new PageEntity() 
  47.             { 
  48.                 UserName = "aaa2"
  49.                 UserSex = "男" 
  50.             }); 
  51.             entitys.Add(new PageEntity() 
  52.             { 
  53.                 UserName = "aaa3"
  54.                 UserSex = "女" 
  55.             }); 
  56.             entitys.Add(new PageEntity() 
  57.             { 
  58.                 UserName = "aaa4"
  59.                 UserSex = "男" 
  60.             }); 
  61.  
  62.             return entitys; 
  63.         } 
  64.          
  65.  
  66.         public string UserName { get; set; } 
  67.  
  68.         public string UserSex { get; set; } 
  69.  
  70.         public int DataCount { get; set; } 
  71.  
  72.  
  73.     } 


PageFilter方法很简单,就通过LInq分下页,你也可以通过其他方式分页调用存储过程等!这个服务写好了。现在要做的就是silverlight 程序里面引用这个服务;

现在来看silverlight页面代码

  1. <UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  x:Class="Silverlight.MainPage" 
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  4.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  5.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  6.     mc:Ignorable="d" 
  7.     d:DesignHeight="300" d:DesignWidth="400"
  8.  
  9.     <Grid x:Name="LayoutRoot" Background="White"
  10.         <StackPanel> 
  11.             <sdk:DataGrid x:Name="dataGrid" Height="200"/> 
  12.             <sdk:DataPager x:Name="pager1" PageSize="2" PageIndexChanged="pager1_PageIndexChanged"></sdk:DataPager> 
  13.         </StackPanel> 
  14.     </Grid> 
  15. </UserControl> 

这个页面就放了一个DataGrid和DataPager;简单说下这个分页控件,点击分页的时候他会触发PageIndexChanged事件,还有在设置分页控件的Source属性的时候他也会触发PageIndexChanged事件!

我的做法就是当服务器返回的数据总数和分页控件的数据总数不一致的时候在从新设置分页控件的Source;贴代码

  1. using System
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Net; 
  5. using System.Windows; 
  6. using System.Windows.Controls; 
  7. using System.Windows.Documents; 
  8. using System.Windows.Input; 
  9. using System.Windows.Media; 
  10. using System.Windows.Media.Animation; 
  11. using System.Windows.Shapes; 
  12. using Silverlight.Test.PageService; 
  13. using System.Windows.Data; 
  14.  
  15. namespace Silverlight 
  16.     public partial class MainPage : UserControl 
  17.     { 
  18.         public MainPage() 
  19.         { 
  20.             InitializeComponent(); 
  21.             Filter(); 
  22.         } 
  23.  
  24.       
  25.         private void pager1_PageIndexChanged(object sender,EventArgs e) 
  26.         { 
  27.              
  28.             Filter(); 
  29.         } 
  30.  
  31.         private void Filter() 
  32.         { 
  33.              
  34.             WebPageServiceClient client = new WebPageServiceClient(); 
  35.             client.PageCountCompleted += new EventHandler<PageCountCompletedEventArgs>(client_PageCountCompleted); 
  36.             client.PageCountAsync(string.Empty); 
  37.         } 
  38.  
  39.         void client_PageCountCompleted(object sender,PageCountCompletedEventArgs e) 
  40.         { 
  41.             if (e.Result < 1) 
  42.                 return
  43.             SetPageSource(e.Result); 
  44.  
  45.             var pageIndex = pager1.PageIndex + 1; 
  46.             WebPageServiceClient client = sender as WebPageServiceClient; 
  47.             client.PageFilterCompleted += new EventHandler<PageFilterCompletedEventArgs>(client_PageFilterCompleted); 
  48.             client.PageFilterasync(pager1.PageSize,pageIndex,string.Empty); 
  49.         } 
  50.  
  51.  
  52.         void client_PageFilterCompleted(object sender,PageFilterCompletedEventArgs e) 
  53.         { 
  54.             dataGrid.ItemsSource = e.Result; 
  55.         } 
  56.  
  57.         private void SetPageSource(int resultCount) 
  58.         { 
  59.             if (pager1.source != null && (pager1.source as PagedCollectionView).TotalItemCount == resultCount) 
  60.                 return
  61.             List<int> source = new List<int>(); 
  62.             for (int i = 0; i < resultCount; i++) 
  63.             { 
  64.                 source.Add(i); 
  65.             } 
  66.  
  67.             PagedCollectionView pagedCollection = new PagedCollectionView(source); 
  68.             pager1.source = pagedCollection; 
  69.         } 
  70.     } 

服务端分页差不多完成!还有其他的方式大家拿出来分享下!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐