热门:网页模板.net视频教程JQueryMVCjsonExtJs源码示例三级联动JQuery菜单
您现在的位置:.Net中文社区>> Silverlight>>正文内容

Silverlight DomainDataSource 查询方法动态指定

发布时间:2010年08月11日点击数: 佚名

网上许多例子都是在前台XAML代码中指定queryname,如何根据传入的参数不同,动态指定查询方法呢,带着这个疑问,也是项目需要,最终从国外一个网站(也可能是我的搜索关键字不准,有写过这方面资料的人不要向我扔砖头呀*_*)找了个例子,加以改进,最终呈现在大家面前。

 1、前台XAML,DomainDataSource定义代码,不指定queryname

  1. <my2:DomainDataSource x:Name="myDataSource" AutoLoad="False"  LoadedData="myDataSource_LoadedData" > 
  2.             <my2:DomainDataSource.DomainContext> 
  3.                 <ria:TestDomainContext></ria:TestDomainContext> 
  4.             </my2:DomainDataSource.DomainContext> 
  5.             <my2:DomainDataSource.SortDescriptors> 
  6.                 <my2:SortDescriptor PropertyPath="ID" Direction="Descending" ></my2:SortDescriptor> 
  7.             </my2:DomainDataSource.SortDescriptors> 
  8.         </my2:DomainDataSource> 

2、后台代码如下,根据查询需求不同,传入不同的参数,查询方法也是动态指定

  1. private TestDomainContext testDomainContext = new TestDomainContext(); 
  2.    
  3.         private void LoadPersonRecord(string orgCode,string key) 
  4.         { 
  5.             try 
  6.             { 
  7.                 if (!key.Equals(string.Empty)) 
  8.                 { 
  9.                     this.testDomainContext = this.myDataSource.DomainContext as TestDomainContext; 
  10.                     this.testDomainContext.PersonLists.Clear(); 
  11.  
  12.                     //查询参数清空,一定要放在QueryName前边,不然会出错 
  13.                     this.myDataSource.QueryParameters.Clear(); 
  14.                     this.myDataSource.QueryName = "GetPersonListByNameQuery"
  15.  
  16.                     var p = new Parameter(); 
  17.                     p.ParameterName = "orgCode"
  18.                     p.Value = orgCode; 
  19.                    
  20.                     this.myDataSource.QueryParameters.Add(p); 
  21.  
  22.                     p = new Parameter(); 
  23.                     p.ParameterName = "key"
  24.                     p.Value = key; 
  25.                     this.myDataSource.QueryParameters.Add(p); 
  26.                     this.myDataSource.Load(); 
  27.                 } 
  28.             } 
  29.             catch (Exception exc) 
  30.             { 
  31.                 this.ControlEnabled(true); 
  32.                 MessageBox.Show("查询时出错,错误提示:" + exc.Message, "提示", MessageBoxButton.OK); 
  33.             } 
  34.         } 
  35.  
  36.         private void LoadPersonRecordByOrgCode(Int64 code) 
  37.         { 
  38.             try 
  39.             { 
  40.                 this.testDomainContext = this.myDataSource.DomainContext as TestDomainContext; 
  41.                 this.testDomainContext.PersonLists.Clear(); 
  42.  
  43.                 //查询参数清空,一定要放在QueryName前边,不然会出错 
  44.                 this.myDataSource.QueryParameters.Clear(); 
  45.                 this.myDataSource.QueryName = "GetPersonListQuery"
  46.  
  47.                 var p = new Parameter(); 
  48.                 p.ParameterName = "orgCode"
  49.                 p.Value = code.ToString(); 
  50.               
  51.                 this.myDataSource.QueryParameters.Add(p); 
  52.                 this.myDataSource.Load(); 
  53.             } 
  54.             catch (Exception exc) 
  55.             { 
  56.                 this.ControlEnabled(true); 
  57.                 MessageBox.Show("查询时出错,错误提示:" + exc.Message, "提示", MessageBoxButton.OK); 
  58.             } 
  59.         } 
  60.   
  61.         private void myDataSource_LoadedData(object sender, LoadedDataEventArgs e) 
  62.         { 
  63.             if (e.HasError) 
  64.             { 
  65.                 System.Windows.MessageBox.Show(e.Error.ToString()); 
  66.                 e.MarkErrorAsHandled(); 
  67.             } 
  68.             else 
  69.             { 
  70.                 List<PersonList> result = this.testDomainContext.PersonLists.ToList<PersonList>(); 
  71.                 PagedCollectionView pcv = new PagedCollectionView(result); 
  72.                 pcv.PageSize = 40;        //每页显示40条数据 
  73.                 this.myGrid.ItemsSource = pcv; 
  74.             } 
  75.         } 

本站热点业务

更多模板/案例展示

关于我们 | 联系我们 | 团队日志 | 网站地图 | 网站合作