Silverlight不写实体类也能显示数据呵呵

开篇

      在任何的BS项目的开发中,怎么把后台的数据展示到前端,都是无法避免的甚至有时候让人头疼的问题。考虑到这点,Silverlight中微软给予了我们Binding这一伟大的特性。借助他,让我们可以用非常优雅的方式处理数据的展示.....。我们知道,Binding是沟通Xaml和数据源的桥梁,而大部分时候,我们的数据源都是一个实体类实例或者实体类实例集合。故在我们潜意识里,要展示一份数据,几乎必须写实体类不可。本文的目的就是使用一种‘巧妙的方式’,让我们不写实体类也可以Bingding.....从而显示数据。

准备

   我们的目的非常简单,写一个方法,传入字符串的JSON数据,让DataGrid显示数据。Json数据如下。特别注意该JSON格式。JSON数据如下。

View Code
[
    {
        "PersonAidsID": "ff9619ff-8b46-d011-b42d-00c04fc934f1",
        "IDNum": "594832934875849485",
        "Sex": "1",
        "RecordTime": "2012/2/28 10:29:34",
        "Address": "合额度126号",
        "Name": "陈-冠-希是你",
        "Age": "34",
        "GeographicLocation": "43.887594849178,81.3012265555597",
        "IllnessNarratives": "2",
        "得病类型": "",
        "民族": "",
        "吸毒史": "",
        "VillageID": "ff9619ff-8b46-d011-b42d-00c04fc934f6"
    },
    {
        "PersonAidsID": "ff9619ff-8b46-d011-b42d-00c04fc934f1",
        "IDNum": "594832934875849485",
        "Sex": "1",
        "RecordTime": "2012/2/28 10:29:34",
        "Address": "合额度126号",
        "Name": "陈-冠-希是你",
        "Age": "34",
        "GeographicLocation": "43.887594849178,81.3012265555597",
        "IllnessNarratives": "2",
        "得病类型": "",
        "民族": "",
        "吸毒史": "",
        "VillageID": "ff9619ff-8b46-d011-b42d-00c04fc934f6"
    },
    {
        "PersonAidsID": "ff9619ff-8b46-d011-b42d-00c04fc934f1",
        "IDNum": "594832934875849485",
        "Sex": "1",
        "RecordTime": "2012/2/28 10:29:34",
        "Address": "合额度126号",
        "Name": "陈-冠-希是你",
        "Age": "34",
        "GeographicLocation": "43.887594849178,81.3012265555597",
        "IllnessNarratives": "2",
        "得病类型": "",
        "民族": "",
        "吸毒史": "",
        "VillageID": "ff9619ff-8b46-d011-b42d-00c04fc934f6"
    },
    {
        "PersonAidsID": "ff9619ff-8b46-d011-b42d-00c04fc934f1",
        "IDNum": "594832934875849485",
        "Sex": "1",
        "RecordTime": "2012/2/28 10:29:34",
        "Address": "合额度126号",
        "Name": "陈-冠-希是你",
        "Age": "34",
        "GeographicLocation": "43.887594849178,81.3012265555597",
        "IllnessNarratives": "2",
        "得病类型": "",
        "民族": "",
        "吸毒史": "",
        "VillageID": "ff9619ff-8b46-d011-b42d-00c04fc934f6"
    },
    {
        "PersonAidsID": "ff9619ff-8b46-d011-b42d-00c04fc934f1",
        "IDNum": "594832934875849485",
        "Sex": "1",
        "RecordTime": "2012/2/28 10:29:34",
        "Address": "合额度126号",
        "Name": "陈-冠-希是你",
        "Age": "34",
        "GeographicLocation": "43.887594849178,81.3012265555597",
        "IllnessNarratives": "2",
        "得病类型": "",
        "民族": "",
        "吸毒史": "",
        "VillageID": "ff9619ff-8b46-d011-b42d-00c04fc934f6"
    },
    {
        "PersonAidsID": "ff9619ff-8b46-d011-b42d-00c04fc934f1",
        "IDNum": "594832934875849485",
        "Sex": "1",
        "RecordTime": "2012/2/28 10:29:34",
        "Address": "合额度126号",
        "Name": "陈-冠-希是你",
        "Age": "34",
        "GeographicLocation": "43.887594849178,81.3012265555597",
        "IllnessNarratives": "2",
        "得病类型": "",
        "民族": "",
        "吸毒史": "",
        "VillageID": "ff9619ff-8b46-d011-b42d-00c04fc934f6"
    },
    {
        "PersonAidsID": "ff9619ff-8b46-d011-b42d-00c04fc934f1",
        "IDNum": "594832934875849485",
        "Sex": "1",
        "RecordTime": "2012/2/28 10:29:34",
        "Address": "合额度126号",
        "Name": "陈-冠-希是你",
        "Age": "34",
        "GeographicLocation": "43.887594849178,81.3012265555597",
        "IllnessNarratives": "2",
        "得病类型": "",
        "民族": "",
        "吸毒史": "",
        "VillageID": "ff9619ff-8b46-d011-b42d-00c04fc934f6"
    },
    {
        "PersonAidsID": "ff9619ff-8b46-d011-b42d-00c04fc934f1",
        "IDNum": "594832934875849485",
        "Sex": "1",
        "RecordTime": "2012/2/28 10:29:34",
        "Address": "合额度126号",
        "Name": "陈-冠-希是你",
        "Age": "34",
        "GeographicLocation": "43.887594849178,81.3012265555597",
        "IllnessNarratives": "2",
        "得病类型": "",
        "民族": "",
        "吸毒史": "",
        "VillageID": "ff9619ff-8b46-d011-b42d-00c04fc934f6"
    },
    {
        "PersonAidsID": "ff9619ff-8b46-d011-b42d-00c04fc934f1",
        "IDNum": "594832934875849485",
        "Sex": "1",
        "RecordTime": "2012/2/28 10:29:34",
        "Address": "合额度126号",
        "Name": "陈-冠-希是你",
        "Age": "34",
        "GeographicLocation": "43.887594849178,81.3012265555597",
        "IllnessNarratives": "2",
        "得病类型": "",
        "民族": "",
        "吸毒史": "",
        "VillageID": "ff9619ff-8b46-d011-b42d-00c04fc934f6"
    }
]

 

 服务端:

   特别注意我的服务端.....  就是一个ASPX的页面。然后使用Response.Write 的方法向客户端输送数据。个人觉得这种方式简单又灵活。比起WCF。

最后的服务端如下

          

 

客户端取得数据:

string dataUrl = HtmlPage.Document.DocumentUri.AbsoluteUri.Replace("Default.aspx", "Handler/Data.aspx");
WebClient webClient = new WebClient();
webClient.DownloadStringAsync(new Uri(dataUrl));
webClient.DownloadStringCompleted += (sender1, e1) =>
{
  MessageBox.Show(e1.Result);
};

 

呵呵这写法非常常见。   但值得各位注意的是:

string dataUrl = HtmlPage.Document.DocumentUri.AbsoluteUri.Replace("Default.aspx", "Handler/Data.aspx");  

这个写法。这样的话,不管换哪个IP,都不会有问题。

反射实体类来咯

      上面客户端已经可以取得数据了。接下来要做的就是怎么把JSON转换为实体类实例集合。

   有三步工作要做。

      1:从JSON中获得列头。(见代码中GetCoumsByJson 方法)

      2:从列头反射形成实体类。(见代码中getTypeByStrList方法)

  3:反序列化形成实体类集合。

  下面直接上代码。怎么做的。看代码便知。

  https://files.cnblogs.com/MySilverlight/SilverlightApplication2.zip

     诸位看看代码便知。注意跑起来之后浏览首页要点击一下那个按钮才显示数据哦

会影响性能吗?

   基本不会。因为即便是使用反射形成实体类。但请注意,这里是使用的Emit的方式。这种反射方式不咋影响性能....。

 

 

后话:这算做是我框架介绍的开篇....  呵呵  一共五偏 呵呵

posted @ 2012-05-24 20:43  银光小子  阅读(2393)  评论(9编辑  收藏  举报