您的位置:首页 > 大数据 > 人工智能

Silverlight DomainDataSource 查询方法动态指定

2010-08-17 10:55 204 查看
来源:/article/5356650.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;
}
}

觉得有用的顶一下呀,呵呵
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐