Silverlight DomainDataSource 查询方法动态指定

来源:http://www.cnblogs.com/zjl880/archive/2010/08/10/1796573.html

 

 

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

 

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

 

< my2:DomainDataSource  x:Name ="myDataSource"   AutoLoad ="False"  LoadedData="myDataSource_LoadedData"   >
            
< my2:DomainDataSource.DomainContext >
                
< ria:TestDomainContext ></ ria:TestDomainContext >
            
</ my2:DomainDataSource.DomainContext >
            
< my2:DomainDataSource.SortDescriptors >
                
< my2:SortDescriptor  PropertyPath ="ID"  Direction ="Descending"   ></ my2:SortDescriptor >
            
</ my2:DomainDataSource.SortDescriptors >
        
</ my2:DomainDataSource>

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

 

private  TestDomainContext testDomainContext  =   new  TestDomainContext();
  
        
private   void  LoadPersonRecord( string  orgCode, string  key)
        {
            
try
            {
                
if  ( ! key.Equals( string .Empty))
                {
                    
this .testDomainContext  =   this .myDataSource.DomainContext  as  TestDomainContext;
                    
this .testDomainContext.PersonLists.Clear();

                    
// 查询参数清空,一定要放在QueryName前边,不然会出错
                     this .myDataSource.QueryParameters.Clear();
                
     this .myDataSource.QueryName  =   " GetPersonListByNameQuery " ;

                    var p 
=   new  Parameter();
                    p.ParameterName 
=   " orgCode " ;
                    p.Value 
=  orgCode;
                  
                    
this .myDataSource.QueryParameters.Add(p);

                    p 
=   new  Parameter();
                    p.ParameterName 
=   " key " ;
                    p.Value 
=  key;
                    
this .myDataSource.QueryParameters.Add(p);
                    
this .myDataSource.Load();
                }
            }
            
catch  (Exception exc)
            {
                
this .ControlEnabled( true );
                MessageBox.Show(
" 查询时出错,错误提示: "   +  exc.Message,  " 提示 " , MessageBoxButton.OK);
            }
        }

        
private   void  LoadPersonRecordByOrgCode(Int64 code)
        {
            
try
            {
                
this .testDomainContext  =   this .myDataSource.DomainContext  as  TestDomainContext;
                
this .testDomainContext.PersonLists.Clear();

                
// 查询参数清空,一定要放在QueryName前边,不然会出错
                 this .myDataSource.QueryParameters.Clear();
           
      this .myDataSource.QueryName  =   " GetPersonListQuery " ;

                var p 
=   new  Parameter();
                p.ParameterName 
=   " orgCode " ;
                p.Value 
=  code.ToString();
             
                
this .myDataSource.QueryParameters.Add(p);
                
this .myDataSource.Load();
            }
            
catch  (Exception exc)
            {
                
this .ControlEnabled( true );
                MessageBox.Show(
" 查询时出错,错误提示: "   +  exc.Message,  " 提示 " , MessageBoxButton.OK);
            }
        }
 

        private void myDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {
            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString());
                e.MarkErrorAsHandled();
            }
            else
            {
                List<PersonList> result = this.testDomainContext.PersonLists.ToList<PersonList>();

                PagedCollectionView pcv = new PagedCollectionView(result);
                pcv.PageSize = 40;        //每页显示40条数据
                this.myGrid.ItemsSource = pcv;
            }
        }

 

觉得有用的顶一下呀,呵呵

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值