silverlight 服务器端分页实现

silverlight 服务器端分页实现,第1张

概述silverlight 服务器端分页实现                分类:            silverlight 2011-10-11 11:58 538人阅读 评论(0) 收藏 举报 silverlight 分页服务端分页就是在服务里面写分页的方法,silverlight 调用的时候传参数过来就行了。先看服务代码! 我这边使用的是wcf服务!类图如下: 服务写了两个方法,PageCo silverlight 服务器端分页实现                分类:            silverlight 2011-10-11 11:58 538人阅读 评论(0) 收藏 举报

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

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

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

[csharp] view plain copy print ? using System;  using System.Collections.Generic;  using System.linq;  using System.Runtime.Serialization;  using System.ServiceModel;  using System.Text;    namespace Web      // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“WebPageService”。      public class WebPageService : IWebPageService      {            public List<PageEntity> PageFilter(int? PageSize,int PageIndex,string filter)          {              List<PageEntity> sources = PageEntity.CreatePageSources();              return sources.Skip((PageSize * (PageIndex - 1)).Value).Take(PageSize.Value).ToList();          }              public int PageCount(string filter)          {              return 6;          }      }        public class PageEntity      {          public static List<PageEntity> CreatePageSources()          {              List<PageEntity> entitys = new List<PageEntity>();              entitys.Add(new PageEntity()              {                  Username="aa"                 UserSex="男"              });              entitys.Add(new PageEntity()              {                  Username = "aaa"                 UserSex = "女"              });              entitys.Add(new PageEntity()              {                  Username = "aaa1"                 UserSex = "女"              });              entitys.Add(new PageEntity()              {                  Username = "aaa2"                 UserSex = "男"              });              entitys.Add(new PageEntity()              {                  Username = "aaa3"                 UserSex = "女"              });              entitys.Add(new PageEntity()              {                  Username = "aaa4"                 UserSex = "男"              });                return entitys;          }                      public string Username { get; set; }            public string UserSex { get; set; }            public int DataCount { get; set; }          } 
using System;using System.Collections.Generic;using System.linq;using System.Runtime.Serialization;using System.ServiceModel;using System.Text;namespace Web{    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“WebPageService”。    public class WebPageService : IWebPageService    {        public List<PageEntity> PageFilter(int? PageSize,int PageIndex,string filter)        {            List<PageEntity> sources = PageEntity.CreatePageSources();            return sources.Skip((PageSize * (PageIndex - 1)).Value).Take(PageSize.Value).ToList();        }        public int PageCount(string filter)        {            return 6;        }    }    public class PageEntity    {        public static List<PageEntity> CreatePageSources()        {            List<PageEntity> entitys = new List<PageEntity>();            entitys.Add(new PageEntity()            {                Username="aa",UserSex="男"            });            entitys.Add(new PageEntity()            {                Username = "aaa",UserSex = "女"            });            entitys.Add(new PageEntity()            {                Username = "aaa1",UserSex = "女"            });            entitys.Add(new PageEntity()            {                Username = "aaa2",UserSex = "男"            });            entitys.Add(new PageEntity()            {                Username = "aaa3",UserSex = "女"            });            entitys.Add(new PageEntity()            {                Username = "aaa4",UserSex = "男"            });            return entitys;        }                public string Username { get; set; }        public string UserSex { get; set; }        public int DataCount { get; set; }    }}


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

现在来看silverlight页面的代码。

[CSS] view plain copy print ? <UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  x:Class="Silverlight.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"         <StackPanel>              <sdk:DataGrID x:name="dataGrID" Height="200"/>              <sdk:DataPager x:name="pager1" PageSize="2" PageIndexChanged="pager1_PageIndexChanged"></sdk:DataPager>          </StackPanel>      </GrID>  </UserControl> 
<UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  x:Class="Silverlight.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">        <StackPanel>            <sdk:DataGrID x:name="dataGrID" Height="200"/>            <sdk:DataPager x:name="pager1" PageSize="2" PageIndexChanged="pager1_PageIndexChanged"></sdk:DataPager>        </StackPanel>    </GrID></UserControl>

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

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

[csharp] view plain copy print ? using System;  using System.Collections.Generic;  using System.linq;  using System.Net;  using System.windows;  using System.windows.Controls;  using System.windows.documents;  using System.windows.input;  using System.windows.Media;  using System.windows.Media.Animation;  using System.windows.Shapes;  using Silverlight.Test.PageService;  using System.windows.Data;    namespace Silverlight      public partial class MainPage : UserControl      {          public MainPage()          {              InitializeComponent();              Filter();          }                   private voID pager1_PageIndexChanged(object sender,EventArgs e)          {                            Filter();          }            private voID Filter()          {                            WebPageServiceClIEnt clIEnt = new WebPageServiceClIEnt();              clIEnt.PageCountCompleted += new EventHandler<PageCountCompletedEventArgs>(clIEnt_PageCountCompleted);              clIEnt.PageCountAsync(string.Empty);          }            voID clIEnt_PageCountCompleted(object sender,PageCountCompletedEventArgs e)          {              if (e.Result < 1)                  return             SetPageSource(e.Result);                var pageIndex = pager1.PageIndex + 1;              WebPageServiceClIEnt clIEnt = sender as WebPageServiceClIEnt;              clIEnt.PageFilterCompleted += new EventHandler<PageFilterCompletedEventArgs>(clIEnt_PageFilterCompleted);              clIEnt.PageFilterasync(pager1.PageSize,pageIndex,string.Empty);          }              voID clIEnt_PageFilterCompleted(object sender,PageFilterCompletedEventArgs e)          {              dataGrID.ItemsSource = e.Result;          }            private voID SetPageSource(int resultCount)          {              if (pager1.source != null && (pager1.source as PagedCollectionVIEw).TotalitemCount == resultCount)                  return             List<int> source = new List<int>();              for (int i = 0; i < resultCount; i++)              {                  source.Add(i);              }                PagedCollectionVIEw pagedCollection = new PagedCollectionVIEw(source);              pager1.source = pagedCollection;          }      } 
using System;using System.Collections.Generic;using System.linq;using System.Net;using System.windows;using System.windows.Controls;using System.windows.documents;using System.windows.input;using System.windows.Media;using System.windows.Media.Animation;using System.windows.Shapes;using Silverlight.Test.PageService;using System.windows.Data;namespace Silverlight{    public partial class MainPage : UserControl    {        public MainPage()        {            InitializeComponent();            Filter();        }             private voID pager1_PageIndexChanged(object sender,EventArgs e)        {                        Filter();        }        private voID Filter()        {                        WebPageServiceClIEnt clIEnt = new WebPageServiceClIEnt();            clIEnt.PageCountCompleted += new EventHandler<PageCountCompletedEventArgs>(clIEnt_PageCountCompleted);            clIEnt.PageCountAsync(string.Empty);        }        voID clIEnt_PageCountCompleted(object sender,PageCountCompletedEventArgs e)        {            if (e.Result < 1)                return;            SetPageSource(e.Result);            var pageIndex = pager1.PageIndex + 1;            WebPageServiceClIEnt clIEnt = sender as WebPageServiceClIEnt;            clIEnt.PageFilterCompleted += new EventHandler<PageFilterCompletedEventArgs>(clIEnt_PageFilterCompleted);            clIEnt.PageFilterasync(pager1.PageSize,string.Empty);        }        voID clIEnt_PageFilterCompleted(object sender,PageFilterCompletedEventArgs e)        {            dataGrID.ItemsSource = e.Result;        }        private voID SetPageSource(int resultCount)        {            if (pager1.source != null && (pager1.source as PagedCollectionVIEw).TotalitemCount == resultCount)                return;            List<int> source = new List<int>();            for (int i = 0; i < resultCount; i++)            {                source.Add(i);            }            PagedCollectionVIEw pagedCollection = new PagedCollectionVIEw(source);            pager1.source = pagedCollection;        }    }}

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

总结

以上是内存溢出为你收集整理的silverlight 服务器端分页实现全部内容,希望文章能够帮你解决silverlight 服务器端分页实现所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1021609.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存