Windows Phone7 快递查询

   

(1)API去友商100里申请

布局代码:

Exp.xaml

<phone:PhoneApplicationPage
x:Class="WindowsPhone_Express.Exp"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="800"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="False">

<!--LayoutRoot 包含放置所有其他頁面的根資料格-->
<Grid x:Name="LayoutRoot">
<controls:Panorama Title="快递查询">

<!--Panorama 項目 1-->
<controls:PanoramaItem Header="快递商选择" Loaded="PanoramaItem_Loaded">
<Grid>
<!--<my:ExpressItem Margin="9,6,6,0" x:Name="expressItem1" VerticalAlignment="Top" />-->
<ListBox Margin="12,6,6,6" x:Name="listBox_Express">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Border Width="50" Height="50"
VerticalAlignment="Top" Background="#FF15B7EF">
<TextBlock Text="{Binding Letter}" HorizontalAlignment="Center" Margin="0,0,0,0" x:Name="Letter"
Foreground="White" FontSize="30"
/>
</Border>

<StackPanel Orientation="Horizontal">
<ItemsControl x:Name="ExpListBox" ItemsSource="{Binding ExpressInfoList}" Margin="0,0,0,0">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Tag="{Binding Code}"
x:Name="ExpName" Content="{Binding Name}" Margin="0,0,0,0" FontSize="40"
Foreground="White"
BorderThickness="0"
HorizontalAlignment="Left"
Click="ExpName_Click"

/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

</Grid>
</controls:PanoramaItem>

<!--Panorama 項目 2-->
<controls:PanoramaItem Header="关于">
<Grid>
<TextBlock Margin="25,58,74,403" Name="textBlock1" Text="本应用用于开发学习" Height="37" FontSize="30" Width="321" />
<TextBlock Height="37" HorizontalAlignment="Left" Margin="23,119,0,342" Name="textBlock2" FontSize="30" Text="如有问题请到我的博客留言" Width="380" />
<TextBlock Height="37" HorizontalAlignment="Left" FontSize="25" Margin="31,381,0,80" Name="textBlock3" Text="http://www.cnblog.com/lsmayday" Width="390" />
<TextBlock Height="37" HorizontalAlignment="Left" FontSize="20" Margin="240,440,0,21" Name="textBlock4" Text="API由友商100提供" Width="169" />
</Grid>
</controls:PanoramaItem>
</controls:Panorama>
</Grid>

<!--Panorama 應用程式不應該顯示 ApplicationBar-->

</phone:PhoneApplicationPage>

复制代码
 1 <phone:PhoneApplicationPage 
2 x:Class="WindowsPhone_Express.Exp"
3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5 xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
6 xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
7 xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"
8 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
9 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
10 mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="800"
11 FontFamily="{StaticResource PhoneFontFamilyNormal}"
12 FontSize="{StaticResource PhoneFontSizeNormal}"
13 Foreground="{StaticResource PhoneForegroundBrush}"
14 SupportedOrientations="Portrait" Orientation="Portrait"
15 shell:SystemTray.IsVisible="False">
16
17 <!--LayoutRoot 包含放置所有其他頁面的根資料格-->
18 <Grid x:Name="LayoutRoot">
19 <controls:Panorama Title="快递查询">
20
21 <!--Panorama 項目 1-->
22 <controls:PanoramaItem Header="快递商选择" Loaded="PanoramaItem_Loaded">
23 <Grid>
24 <!--<my:ExpressItem Margin="9,6,6,0" x:Name="expressItem1" VerticalAlignment="Top" />-->
25 <ListBox Margin="12,6,6,6" x:Name="listBox_Express">
26 <ListBox.ItemTemplate>
27 <DataTemplate>
28 <StackPanel Orientation="Horizontal">
29 <Border Width="50" Height="50"
30 VerticalAlignment="Top" Background="#FF15B7EF">
31 <TextBlock Text="{Binding Letter}" HorizontalAlignment="Center" Margin="0,0,0,0" x:Name="Letter"
32 Foreground="White" FontSize="30"
33 />
34 </Border>
35
36 <StackPanel Orientation="Horizontal">
37 <ItemsControl x:Name="ExpListBox" ItemsSource="{Binding ExpressInfoList}" Margin="0,0,0,0">
38 <ItemsControl.ItemTemplate>
39 <DataTemplate>
40 <Button Tag="{Binding Code}"
41 x:Name="ExpName" Content="{Binding Name}" Margin="0,0,0,0" FontSize="40"
42 Foreground="White"
43 BorderThickness="0"
44 HorizontalAlignment="Left"
45 Click="ExpName_Click"
46
47 />
48 </DataTemplate>
49 </ItemsControl.ItemTemplate>
50 </ItemsControl>
51
52 </StackPanel>
53 </StackPanel>
54 </DataTemplate>
55 </ListBox.ItemTemplate>
56 </ListBox>
57
58 </Grid>
59 </controls:PanoramaItem>
60
61 <!--Panorama 項目 2-->
62 <controls:PanoramaItem Header="关于">
63 <Grid>
64 <TextBlock Margin="25,58,74,403" Name="textBlock1" Text="本应用用于开发学习" Height="37" FontSize="30" Width="321"/>
65 <TextBlock Height="37" HorizontalAlignment="Left" Margin="23,119,0,342" Name="textBlock2" FontSize="30" Text="如有问题请到我的博客留言" Width="380"/>
66 <TextBlock Height="37" HorizontalAlignment="Left" FontSize="25" Margin="31,381,0,80" Name="textBlock3" Text="http://www.cnblog.com/lsmayday" Width="390"/>
67 <TextBlock Height="37" HorizontalAlignment="Left" FontSize="20" Margin="240,440,0,21" Name="textBlock4" Text="API由友商100提供" Width="169"/>
68 </Grid>
69 </controls:PanoramaItem>
70 </controls:Panorama>
71 </Grid>
72
73 <!--Panorama 應用程式不應該顯示 ApplicationBar-->
74
75 </phone:PhoneApplicationPage>
复制代码

SelectExp.xaml

<phone:PhoneApplicationPage
x:Class="WindowsPhone_Express.SelectExp"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True"
Loaded="PhoneApplicationPage_Loaded"
>

<!--LayoutRoot 是放置所有頁面的根資料格-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

<!--TitlePanel 包含應用程式的名稱和頁面標題-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="快递查询" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - 其他內容置於此-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Height="30" HorizontalAlignment="Left" Margin="12,59,0,0" Name="textBlock1" Text="单号:" VerticalAlignment="Top" />
<TextBox Margin="63,36,6,0"
BorderThickness="0"
Background="Beige"
Name="textBox_ExpNumber" Text="" Height="72" VerticalAlignment="Top" />
<Button Background="Beige" Foreground="Black" BorderThickness="0" Content="查询" Height="72" Margin="290,114,6,0" Name="button_Ok" VerticalAlignment="Top" Click="button_Ok_Click" />
<ProgressBar x:Name="ProgressBar_Loading" Visibility="Collapsed" Margin="12,114,172,0" IsIndeterminate="True" Height="72" VerticalAlignment="Top"></ProgressBar>
<ListBox x:Name="ExpInfo_List" Margin="12,192,6,6" ItemsSource="{Binding ExpressInfoDataList}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Margin="0" >
<TextBlock x:Name="Text_Date" TextAlignment="Left" Margin="0,0,0,0" Height="35" Padding="0" FontSize="25" Text="{Binding Time}" Foreground="#FF17AEDE"></TextBlock>
<ScrollViewer ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Disabled" Height="50">
<TextBlock x:Name="Text_Content" Margin="0,0,0,0" FontSize="35" Text="{Binding Context}"></TextBlock>
</ScrollViewer>

</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

</Grid>
</Grid>

<!--顯示 ApplicationBar 使用方式的程式碼範例-->
<!--<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="按鈕 1"/>
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="按鈕 2"/>
<shell:ApplicationBar.MenuItems>
<shell:ApplicationBarMenuItem Text="功能表項目 1"/>
<shell:ApplicationBarMenuItem Text="功能表項目 2"/>
</shell:ApplicationBar.MenuItems>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>-->

</phone:PhoneApplicationPage>

复制代码
 1 <phone:PhoneApplicationPage 
2 x:Class="WindowsPhone_Express.SelectExp"
3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5 xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
6 xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
7 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
8 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
9 FontFamily="{StaticResource PhoneFontFamilyNormal}"
10 FontSize="{StaticResource PhoneFontSizeNormal}"
11 Foreground="{StaticResource PhoneForegroundBrush}"
12 SupportedOrientations="Portrait" Orientation="Portrait"
13 mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
14 shell:SystemTray.IsVisible="True"
15 Loaded="PhoneApplicationPage_Loaded"
16 >
17
18 <!--LayoutRoot 是放置所有頁面的根資料格-->
19 <Grid x:Name="LayoutRoot" Background="Transparent">
20 <Grid.RowDefinitions>
21 <RowDefinition Height="Auto"/>
22 <RowDefinition Height="*"/>
23 </Grid.RowDefinitions>
24
25 <!--TitlePanel 包含應用程式的名稱和頁面標題-->
26 <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
27 <TextBlock x:Name="ApplicationTitle" Text="快递查询" Style="{StaticResource PhoneTextNormalStyle}"/>
28 <TextBlock x:Name="PageTitle" Text="" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
29 </StackPanel>
30 <!--ContentPanel - 其他內容置於此-->
31 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
32 <TextBlock Height="30" HorizontalAlignment="Left" Margin="12,59,0,0" Name="textBlock1" Text="单号:" VerticalAlignment="Top"/>
33 <TextBox Margin="63,36,6,0"
34 BorderThickness="0"
35 Background="Beige"
36 Name="textBox_ExpNumber" Text="" Height="72" VerticalAlignment="Top"/>
37 <Button Background="Beige" Foreground="Black" BorderThickness="0" Content="查询" Height="72" Margin="290,114,6,0" Name="button_Ok" VerticalAlignment="Top" Click="button_Ok_Click"/>
38 <ProgressBar x:Name="ProgressBar_Loading" Visibility="Collapsed" Margin="12,114,172,0" IsIndeterminate="True" Height="72" VerticalAlignment="Top"></ProgressBar>
39 <ListBox x:Name="ExpInfo_List" Margin="12,192,6,6" ItemsSource="{Binding ExpressInfoDataList}">
40 <ListBox.ItemTemplate>
41 <DataTemplate>
42 <StackPanel Orientation="Vertical" Margin="0">
43 <TextBlock x:Name="Text_Date" TextAlignment="Left" Margin="0,0,0,0" Height="35" Padding="0" FontSize="25" Text="{Binding Time}" Foreground="#FF17AEDE"></TextBlock>
44 <ScrollViewer ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Disabled" Height="50">
45 <TextBlock x:Name="Text_Content" Margin="0,0,0,0" FontSize="35" Text="{Binding Context}"></TextBlock>
46 </ScrollViewer>
47
48 </StackPanel>
49 </DataTemplate>
50 </ListBox.ItemTemplate>
51 </ListBox>
52
53 </Grid>
54 </Grid>
55
56 <!--顯示 ApplicationBar 使用方式的程式碼範例-->
57 <!--<phone:PhoneApplicationPage.ApplicationBar>
58 <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
59 <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="按鈕 1"/>
60 <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="按鈕 2"/>
61 <shell:ApplicationBar.MenuItems>
62 <shell:ApplicationBarMenuItem Text="功能表項目 1"/>
63 <shell:ApplicationBarMenuItem Text="功能表項目 2"/>
64 </shell:ApplicationBar.MenuItems>
65 </shell:ApplicationBar>
66 </phone:PhoneApplicationPage.ApplicationBar>-->
67
68 </phone:PhoneApplicationPage>
复制代码


Exp.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml;
using System.Xml.Linq;
using System.Collections.ObjectModel;
namespace WindowsPhone_Express
{
public partial class Exp : PhoneApplicationPage
{
public Exp()
{
InitializeComponent();
}

/// <summary>
/// 读取快递列表数据
/// </summary>
private void Reader()
{
ObservableCollection<Cls.Express> Exp = new ObservableCollection<Cls.Express>();

XDocument Xdocument = XDocument.Load("Xml_Data/Express.xml");
foreach (XElement Xelement in Xdocument.Element("Data").Descendants("Express"))
{
Cls.Express ExpInfo = new Cls.Express();
ExpInfo.Letter = Xelement.Attribute("Letter").Value.ToString();

ExpInfo.ExpressInfoList = new List<Cls.ExpressInfo>();
foreach (XElement Xnode in Xelement.Nodes())
{
ExpInfo.ExpressInfoList.Add(new Cls.ExpressInfo { Code = Xnode.Attribute("Code").Value.ToString(), Name = Xnode.Value.ToString() });
}
Exp.Add(ExpInfo);
}
this.listBox_Express.ItemsSource = Exp;
}

private void ExpName_Click(object sender, RoutedEventArgs e)
{
if (!e.OriginalSource.Equals(null))
{
Button But = (Button)e.OriginalSource;
string Code = But.Tag.ToString();
string Name = But.Content.ToString();
this.NavigationService.Navigate(new Uri(string.Format("/SelectExp.xaml?Code={0}&Name={1}",
Code,
Name
), UriKind.Relative));
}

}

private void PanoramaItem_Loaded(object sender, RoutedEventArgs e)
{
Reader();
}
}
}

复制代码
 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Net;
5 using System.Windows;
6 using System.Windows.Controls;
7 using System.Windows.Documents;
8 using System.Windows.Input;
9 using System.Windows.Media;
10 using System.Windows.Media.Animation;
11 using System.Windows.Shapes;
12 using Microsoft.Phone.Controls;
13 using System.Xml;
14 using System.Xml.Linq;
15 using System.Collections.ObjectModel;
16 namespace WindowsPhone_Express
17 {
18 public partial class Exp : PhoneApplicationPage
19 {
20 public Exp()
21 {
22 InitializeComponent();
23 }
24
25 ///<summary>
26 /// 读取快递列表数据
27 ///</summary>
28 private void Reader()
29 {
30 ObservableCollection<Cls.Express> Exp = new ObservableCollection<Cls.Express>();
31
32 XDocument Xdocument = XDocument.Load("Xml_Data/Express.xml");
33 foreach (XElement Xelement in Xdocument.Element("Data").Descendants("Express"))
34 {
35 Cls.Express ExpInfo = new Cls.Express();
36 ExpInfo.Letter = Xelement.Attribute("Letter").Value.ToString();
37
38 ExpInfo.ExpressInfoList = new List<Cls.ExpressInfo>();
39 foreach (XElement Xnode in Xelement.Nodes())
40 {
41 ExpInfo.ExpressInfoList.Add(new Cls.ExpressInfo { Code = Xnode.Attribute("Code").Value.ToString(), Name = Xnode.Value.ToString() });
42 }
43 Exp.Add(ExpInfo);
44 }
45 this.listBox_Express.ItemsSource = Exp;
46 }
47
48
49
50 private void ExpName_Click(object sender, RoutedEventArgs e)
51 {
52 if (!e.OriginalSource.Equals(null))
53 {
54 Button But = (Button)e.OriginalSource;
55 string Code = But.Tag.ToString();
56 string Name = But.Content.ToString();
57 this.NavigationService.Navigate(new Uri(string.Format("/SelectExp.xaml?Code={0}&Name={1}",
58 Code,
59 Name
60 ), UriKind.Relative));
61 }
62
63 }
64
65 private void PanoramaItem_Loaded(object sender, RoutedEventArgs e)
66 {
67 Reader();
68 }
69 }
70 }
复制代码



 SelectExp.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml;
using System.Xml.Linq;
using System.Threading;
using System.IO;
using System.Collections.ObjectModel;
namespace WindowsPhone_Express
{
public partial class SelectExp : PhoneApplicationPage
{
Cls.ExpressInfo ExpInfo = new Cls.ExpressInfo();
string Api_Url = "http://api.kuaidi100.com/api?id={0}&com={1}&nu={2}&show=1&muti=1&order=desc";
/// <summary>
/// 快递号
/// </summary>
string ExpNumber = "";
public SelectExp()
{
InitializeComponent();
}

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
if (this.NavigationContext.QueryString["Code"] != null && this.NavigationContext.QueryString["Name"] != null)
{
ExpInfo
.Code = this.NavigationContext.QueryString["Code"].ToString();
ExpInfo
.Name = this.NavigationContext.QueryString["Name"].ToString();
}
else
{
this.NavigationService.GoBack();
}
this.PageTitle.Text = this.ExpInfo.Name;
}

private void button_Ok_Click(object sender, RoutedEventArgs e)
{
if (this.textBox_ExpNumber.Text.ToString().Trim().Equals(""))
{
MessageBox.Show("请输入单号", "错误提示", MessageBoxButton.OK);
}
else {
this.ProgressBar_Loading.Visibility = System.Windows.Visibility.Visible;
this.textBox_ExpNumber.IsEnabled = false;
this.button_Ok.IsEnabled = false;
ExpNumber = this.textBox_ExpNumber.Text;

this.ExpInfo_List.ItemsSource = null;
new Thread(new ThreadStart(ResponseApi)).Start();

}
}


#region
delegate void ResponseXmldelegate(
ObservableCollection<Cls.ExpressInfoList> List
);
/// <summary>
/// 请求API,返回XML文档
/// </summary>
private void ResponseApi() {
string Url = string.Format(Api_Url,
new Config().Key,
this.ExpInfo.Code,
ExpNumber
).ToString();

GetHttpHtml(Url);

}
private void ShowExpInfoList(ObservableCollection<Cls.ExpressInfoList> List)
{
this.textBox_ExpNumber.IsEnabled = true;
this.button_Ok.IsEnabled = true;
this.ProgressBar_Loading.Visibility = System.Windows.Visibility.Collapsed;
if (List[0].Status.Equals("0"))
{
MessageBox.Show(List[0].Message.ToString(), "错误提示", MessageBoxButton.OK);
}
else {
this.ExpInfo_List.ItemsSource = List[0].DataList;
}
}
#endregion

/// <summary>
/// 返回指定URL页面内容
/// </summary>
/// <param name="Url"></param>
/// <returns></returns>
private void GetHttpHtml(string Url) {

WebRequest request = WebRequest.Create(Url);
request.BeginGetResponse(new AsyncCallback(HttpHtmlResponse), request);


}
private void HttpHtmlResponse(IAsyncResult request)
{
HttpWebRequest HttpRequest = (HttpWebRequest)request.AsyncState;
HttpWebResponse HttpResponse = (HttpWebResponse)HttpRequest.EndGetResponse(request);
using (Stream _Stream = HttpResponse.GetResponseStream()) {

ObservableCollection<Cls.ExpressInfoList> ExpList = new ObservableCollection<Cls.ExpressInfoList>();
XDocument Xdoc = XDocument.Load(_Stream);
Cls.ExpressInfoList InfoList = new Cls.ExpressInfoList();

InfoList.Message = Xdoc.Element("xml").Element("message").Value.ToString();
InfoList.Status = Xdoc.Element("xml").Element("status").Value.ToString();
InfoList.DataList = new List<Cls.ExpressInfoDataList>();
foreach (XElement Xelement in Xdoc.Element("xml").Descendants("data"))
{
InfoList.DataList.Add(new Cls.ExpressInfoDataList { Context = Xelement.Element("context").Value.ToString(), Time = Xelement.Element("time").Value.ToString() });
}
ExpList.Add(InfoList);
Dispatcher.BeginInvoke(new ResponseXmldelegate(ShowExpInfoList), ExpList);
}
}
}
}

复制代码
  1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Net;
5 using System.Windows;
6 using System.Windows.Controls;
7 using System.Windows.Documents;
8 using System.Windows.Input;
9 using System.Windows.Media;
10 using System.Windows.Media.Animation;
11 using System.Windows.Shapes;
12 using Microsoft.Phone.Controls;
13 using System.Xml;
14 using System.Xml.Linq;
15 using System.Threading;
16 using System.IO;
17 using System.Collections.ObjectModel;
18 namespace WindowsPhone_Express
19 {
20 public partial class SelectExp : PhoneApplicationPage
21 {
22 Cls.ExpressInfo ExpInfo = new Cls.ExpressInfo();
23 string Api_Url = "http://api.kuaidi100.com/api?id={0}&com={1}&nu={2}&show=1&muti=1&order=desc";
24 ///<summary>
25 /// 快递号
26 ///</summary>
27 string ExpNumber = "";
28 public SelectExp()
29 {
30 InitializeComponent();
31 }
32
33 private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
34 {
35 if (this.NavigationContext.QueryString["Code"] != null && this.NavigationContext.QueryString["Name"] != null)
36 {
37 ExpInfo
38 .Code = this.NavigationContext.QueryString["Code"].ToString();
39 ExpInfo
40 .Name = this.NavigationContext.QueryString["Name"].ToString();
41 }
42 else
43 {
44 this.NavigationService.GoBack();
45 }
46 this.PageTitle.Text = this.ExpInfo.Name;
47 }
48
49 private void button_Ok_Click(object sender, RoutedEventArgs e)
50 {
51 if (this.textBox_ExpNumber.Text.ToString().Trim().Equals(""))
52 {
53 MessageBox.Show("请输入单号", "错误提示", MessageBoxButton.OK);
54 }
55 else {
56 this.ProgressBar_Loading.Visibility = System.Windows.Visibility.Visible;
57 this.textBox_ExpNumber.IsEnabled = false;
58 this.button_Ok.IsEnabled = false;
59 ExpNumber = this.textBox_ExpNumber.Text;
60
61 this.ExpInfo_List.ItemsSource = null;
62 new Thread(new ThreadStart(ResponseApi)).Start();
63
64 }
65 }
66
67
68 #region
69 delegate void ResponseXmldelegate(
70 ObservableCollection<Cls.ExpressInfoList> List
71 );
72 ///<summary>
73 /// 请求API,返回XML文档
74 ///</summary>
75 private void ResponseApi() {
76 string Url = string.Format(Api_Url,
77 new Config().Key,
78 this.ExpInfo.Code,
79 ExpNumber
80 ).ToString();
81
82 GetHttpHtml(Url);
83
84 }
85 private void ShowExpInfoList(ObservableCollection<Cls.ExpressInfoList> List)
86 {
87 this.textBox_ExpNumber.IsEnabled = true;
88 this.button_Ok.IsEnabled = true;
89 this.ProgressBar_Loading.Visibility = System.Windows.Visibility.Collapsed;
90 if (List[0].Status.Equals("0"))
91 {
92 MessageBox.Show(List[0].Message.ToString(), "错误提示", MessageBoxButton.OK);
93 }
94 else {
95 this.ExpInfo_List.ItemsSource = List[0].DataList;
96 }
97 }
98 #endregion
99
100 ///<summary>
101 /// 返回指定URL页面内容
102 ///</summary>
103 ///<param name="Url"></param>
104 ///<returns></returns>
105 private void GetHttpHtml(string Url) {
106
107 WebRequest request = WebRequest.Create(Url);
108 request.BeginGetResponse(new AsyncCallback(HttpHtmlResponse), request);
109
110
111 }
112 private void HttpHtmlResponse(IAsyncResult request)
113 {
114 HttpWebRequest HttpRequest = (HttpWebRequest)request.AsyncState;
115 HttpWebResponse HttpResponse = (HttpWebResponse)HttpRequest.EndGetResponse(request);
116 using (Stream _Stream = HttpResponse.GetResponseStream()) {
117
118 ObservableCollection<Cls.ExpressInfoList> ExpList = new ObservableCollection<Cls.ExpressInfoList>();
119 XDocument Xdoc = XDocument.Load(_Stream);
120 Cls.ExpressInfoList InfoList = new Cls.ExpressInfoList();
121
122 InfoList.Message = Xdoc.Element("xml").Element("message").Value.ToString();
123 InfoList.Status = Xdoc.Element("xml").Element("status").Value.ToString();
124 InfoList.DataList = new List<Cls.ExpressInfoDataList>();
125 foreach (XElement Xelement in Xdoc.Element("xml").Descendants("data"))
126 {
127 InfoList.DataList.Add(new Cls.ExpressInfoDataList { Context = Xelement.Element("context").Value.ToString(), Time = Xelement.Element("time").Value.ToString() });
128 }
129 ExpList.Add(InfoList);
130 Dispatcher.BeginInvoke(new ResponseXmldelegate(ShowExpInfoList), ExpList);
131 }
132 }
133 }
134 }
复制代码
原文地址:https://www.cnblogs.com/CharlesGrant/p/3639337.html