zoukankan      html  css  js  c++  java
  • windows phone数据网络开发

    LINQ

      LINQ的全称是Language INtegrated Query,即语言集成查询。LINQ是一种查询语言,不仅可以对数字库进行查询,还可以对.net的数据集、数组、Xml文档等对象进行查询,并支持对这些数据源对象进行查询后的排序、分组、和结构化等常见的查询操作。

      LINQ位于using System.Xml.Linq命名空间下。using System.Xml.Linq命名空间需要协同using System.Xml才能完成Linq to Xml的引用。

      使用LINQ时需要引入using System.Xml.Linq和using System.Xml命名空间。

      LINQ的查询操作通常由以下3个不同的操作组成。

        (1)获取数据源

        (2)创建查询

        (3)执行查询

      例如下面的代码:

    1 sting[] a={"one","two","three","four"};
    2 var result=
    3     from str in a
    4     where str.length==3
    5     select str;
    6 foreach(var str in result)
    7 {
    8     Console.Write(str);
    9 }
    View Code

      查询分为两种:
        一种是延迟执行,在定义完成查询变量后,实际的查询执行会延迟到在foreach语句中循环访问查询变量时发生。
        另一种是强制立即执行,对一系列源元素执行聚合函数的查询必须首先访问这些元素。

      LINQ到SQL的使用主要可以分为两大步骤:
        1.创建对象模型
        2.使用对象模型
        使用对象模型的典型步骤:
        (1)创建查询以从数据库中检索信息
        (2)重写Insert、Update、和Delete的默认行为
        (3)设置适当的选项以检测和报告并发冲突
        (4)建立继承层次结构
        (5)提供合适的用户界面
        (6)调试并测试应用程序

      如下例,显示小于 5的数
      主要实现代码:

        MainPage.xaml

    1  <Grid x:Name="ContentPanel" Grid.Row="1" Margin="0,10,24,-10">
    2       <StackPanel Name="Showsp" Height="508" HorizontalAlignment="Left" VerticalAlignment="Top"Width="438" Margin="8,0,0,0">
    3       </StackPanel>
    4       <Button Name="SButton" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="164,513,0,0" Content="显示小于5的数" Click="SButton_Click_1" />
    5  </Grid>
    View Code

        MainPage.cs

     1 private void SButton_Click_1(object sender, RoutedEventArgs e)
     2 {
     3     int[] number = new int[7]{ 1,6,3,2,5,3,7};
     4     var numQuery =
     5       from num in number
     6       where num <5
     7       select num;
     8      foreach (int num in numQuery)
     9     { 
    10         TextBlock tex=new TextBlock();
    11         tex.Text = num.ToString ();
    12         tex.Margin=new Thickness(2,2,2,2);
    13         this.Showsp.Children.Add(tex);
    14     }
    15 }
    View Code

    使用XML

      windows phoen提供了两种XML解释方式:一种是XmlReader类,一种是使用LINQ to XML。XmlReader适合读取较大的XML,数据较少的或者需要使用缓存技术的可以使用LINQ to XML。
      XmlReader类位于System.Xml命名空间下,使用时需要引用命名空间。
      XmlReader解释数据时只能以顺序的方式从上往下进行读取。XmlReader能够从Stream、TextReader、独立存储空间应用程序包中读取XML数据,然后使用          XmlReader.Create方法创建XmlReader.阅读器,就可以使用Read方法对XML进行读取和解释了。 

    WebClient

      WebClient类能够完成本地数据上传和远程数据下载。使用WebClient进行上传或下载数据时是以异步方式进的。
      一般过程是首先提供一个资源Url,实例化上传或下载请求、监事上传或下载进度、上传或下载完成后要处理的操作。换句话说WebClient使用事件来处理上传或下载完成时逻辑操作,使用方法来完成数据的上传或下载。

      UploadStringAsync(Uri, String)方法,将指定的字符串上载到指定的资源。此方法不会阻止调用线程。  public void UploadStringAsync(Uri address,string data):此方法将字符串发送到资源。该字符串是使用从线程池自动分配的线程资源异步发送的。在上载字符串之前,此方法使用 Encoding 属性中指定的编码方法将该字符串转换为 Byte 数组。若要在字符串的上载完成时收到通知,可以为 UploadStringCompleted 事件添加事件处理程序。
    可以使用 CancelAsync 方法取消尚未完成的异步操作。

      
      DownloadStringAsync(Uri)方法 ,以字符串形式下载位于指定 Uri 的资源。public void DownloadStringAsync(Uri address):资源是异步下载的。当下载完成时,将引发 DownloadStringCompleted 事件。已下载的字符串在 DownloadStringCompletedEventArgs 的 Result 属性中提供。在完成第一个字符串下载操作前,不能对同一 WebClient 对象再次调用 DownloadStringAsync 方法。执行此操作会导致异常。
      可以使用 CancelAsync 方法取消尚未完成的异步操作。

      OpenWriteAsync(Uri)方法,打开一个流以将数据写入指定的资源。此方法不会阻止调用线程。public void OpenWriteAsync(Uri address,string method)

      OpenReadAsync(Uri)方法,打开流向指定资源的可读流。public void OpenReadAsync(Uri address) 在完成第一个下载操作前,不能对同一 WebClient 对象再次调用 OpenReadAsync 方法。执行此操作会导致异常。

      
      WebClient常见的处理事件如下:
        1.DownloadProgressChanged:监视下载进度。
        2.DownloadStringCompleted:下载字符串数据完成时触发。
        3.UploadProgressChanged:监视上传进度。
        4.UploadStringCompleted:上传字符串完成时触发。
        5.OpenReadCompleted:读取包资源时触发。
        6.OpenWriteCompleted:写入包资源时触发。

      实例如下:
        下载一个远程的XML文件该文件定义了两张图片,然后将图片显示出来
        主要代码如下:
          MainPage.xaml

    1 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    2        <StackPanel Height="421" HorizontalAlignment="Left" Margin="33,22,0,0" Name="ShowImg" VerticalAlignment="Top" Width="406" />
    3      <Button Content="下载图片" Height="68" HorizontalAlignment="Left"Margin="105,478,0,0" Name="button1"VerticalAlignment="Top" Width="233" Click="button1_Click" />
    4 </Grid>
    View Code

          MainPage.xaml.cs

     1 private void button1_Click(object sender, RoutedEventArgs e)
     2 {
     3     //声明xml地址 
     4     Uri xmlurl = new Uri("http://xml/1.xml",UriKind.RelativeOrAbsolute);   
     5     //声明webclient对像        
     6     WebClient client = new WebClient();     
     7     //DownloadStringAsync进行异步下载        
     8     client.DownloadStringAsync(xmlurl);
     9     //下载后触发读取事件
    10     client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
    11 }
    12 
    13 void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    14 {           
    15     //XmlReader创建XML解释器
    16     XmlReader xreadr = XmlReader.Create(new StringReader(e.Result.ToString()));    
    17     //使用XDocument处理XML      
    18     XDocument xdoc = XDocument.Load(xreadr);         
    19      //使用LINQ解释XML
    20      List<Uri> imgesuri = (
    21      from image in xdoc.Descendants("image")//在"image"节点下查找    
    22    select new Uri(image.Attribute("address").Value, 
    23          UriKind.RelativeOrAbsolute)//在image节点查找address,并生成Uri类型数据
    24      ).ToList();//最后转换成序列化数据
    25 
    26      //执行LINQ
    27      foreach (Uri uri in imgesuri)
    28       {
    29                //创建一个图片
    30                 Image img = new Image();          
    31                 img.Width = 200;
    32                 img.Height=200;
    33                 img.Source = new BitmapImage(uri);//指定其地址
    34            this.ShowImg.Children.Add(img);//为显示控件添加UI元素
    35     }         
    36 }
    View Code


     

  • 相关阅读:
    volley框架使用
    Insert Interval
    candy(贪心)
    Best Time to Buy and Sell Stock
    Best Time to Buy and Sell Stock III
    distinct subsequences
    edit distance(编辑距离,两个字符串之间相似性的问题)
    trapping rain water
    word break II(单词切分)
    sudoku solver(数独)
  • 原文地址:https://www.cnblogs.com/spilledlight/p/4883862.html
Copyright © 2011-2022 走看看