学习wp好长一段时间了,做了很多的小项目 。和大家一起分享一下心得 。
列车时刻表查询 是我的其中一个小项目,通过调用网上的免费的web服务完成信息的获取,非常感谢 http://webxml.com.cn/网站 为我们这些学生程序员提供了很多免费的服务。
里面的很多服务都可以利用 做出自己的小项目来,通过这个简单的车次查询小应用, 希望给热爱wp程序开发的同学带来开放程序的热情,用自己的创新思维开发新的应用,为wp应用世界不断辉煌贡献自己的力量。谢谢...
开始啦 .......
vs2010
blend
模拟器打开了没?
分享自己的一点心得 ,每当我想做小项目的时候我都会在一张纸上勾画一下界面 这样在用blend的时候可以很快的把项目的界面给大家起来 这样我们就可以很快的投入到代码的编写了。
我想大家一定都会理解我画的界面 虽然不是很好看(我不是美工哦)希望能理解我每个箭头所指的应用什么控件 ,顺时针 Pagetitle(车次查询) TextBox ListBox applicationbarIcon textBlocks
这是设计好的界面
在这里最重要的是设计listbox的项模版和数据模版.
讲讲我在blend中如何设计的.
由于我是设计完了所以选择编辑当前项.
这里的TrainStation arriveTime StartTime KM 是TextBlock的名字 这样命名 是和你的类名一样的 这样就不会在绑定数据的时候出错哦 !
当你设计好了这个界面以后就可以单击 这个来完成设计了.
界面设计好了以后想必大家都期待写代码了吧。先别急 数据源咱们还没分析呢
打开我给大家推荐的网站
http://webxml.com.cn/
找到
web服务 http://webservice.webxml.com.cn/WebServices/TrainTimeWebService.asmx
打开这个服务 大家会看到很多的调用方法 如何去使用这些方法呢
。。。。。。。
用vs打开我们刚才设计好的项目,添加服务
在地址栏中输入web服务地址哦!
添加成功完服务后我们来分析我们应该使用那个方法呢 这么多的方法想必大家都想使用 做的应用越强大越好,现在我在这里只说说
getDetailInfoByTrainCode
这个方法吧 ,
单击调用会在ie中
<?
xml version="1.0" encoding="utf-8"
?>
-
<
DataSet
xmlns
="
http://WebXml.com.cn/
"
>
-
<
xs:schema
id
="
getDetailInfo
"
xmlns
="
"
xmlns:xs
="
http://www.w3.org/2001/XMLSchema
"
xmlns:msdata
="
urn:schemas-microsoft-com:xml-msdata
"
>
-
<
xs:element
name
="
getDetailInfo
"
msdata:IsDataSet
="
true
"
msdata:UseCurrentLocale
="
true
"
>
-
<
xs:choice
minOccurs
="
0
"
maxOccurs
="
unbounded
"
>
-
<
xs:element
name
="
TrainDetailInfo
"
>
<
xs:element
name
="
TrainStation
"
type
="
xs:string
"
minOccurs
="
0
"
/>
<
xs:element
name
="
ArriveTime
"
type
="
xs:string
"
minOccurs
="
0
"
/>
<
xs:element
name
="
StartTime
"
type
="
xs:string
"
minOccurs
="
0
"
/>
<
xs:element
name
="
KM
"
type
="
xs:short
"
minOccurs
="
0
"
/>
</
xs:sequence
>
</
xs:complexType
>
</
xs:element
>
</
xs:choice
>
</
xs:complexType
>
</
xs:element
>
</
xs:schema
>
-
<
diffgr:diffgram
xmlns:msdata
="
urn:schemas-microsoft-com:xml-msdata
"
xmlns:diffgr
="
urn:schemas-microsoft-com:xml-diffgram-v1
"
>
-
<
getDetailInfo
xmlns
="
"
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo1
"
msdata:rowOrder
="
0
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
哈尔滨(车次:K1124\K1121)
</
TrainStation
>
<
StartTime
>
17:35:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo2
"
msdata:rowOrder
="
1
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
兰棱
</
TrainStation
>
<
ArriveTime
>
18:25:00
</
ArriveTime
>
<
StartTime
>
18:26:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo3
"
msdata:rowOrder
="
2
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
长春
</
TrainStation
>
<
ArriveTime
>
20:14:00
</
ArriveTime
>
<
StartTime
>
20:24:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo4
"
msdata:rowOrder
="
3
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
公主岭
</
TrainStation
>
<
ArriveTime
>
21:02:00
</
ArriveTime
>
<
StartTime
>
21:04:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo5
"
msdata:rowOrder
="
4
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
四平
</
TrainStation
>
<
ArriveTime
>
21:43:00
</
ArriveTime
>
<
StartTime
>
21:46:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo6
"
msdata:rowOrder
="
5
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
铁岭
</
TrainStation
>
<
ArriveTime
>
23:01:00
</
ArriveTime
>
<
StartTime
>
23:04:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo7
"
msdata:rowOrder
="
6
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
沈阳北
</
TrainStation
>
<
ArriveTime
>
00:24:00
</
ArriveTime
>
<
StartTime
>
00:40:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo8
"
msdata:rowOrder
="
7
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
大虎山
</
TrainStation
>
<
ArriveTime
>
02:50:00
</
ArriveTime
>
<
StartTime
>
02:53:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo9
"
msdata:rowOrder
="
8
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
沟帮子
</
TrainStation
>
<
ArriveTime
>
03:21:00
</
ArriveTime
>
<
StartTime
>
03:24:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo10
"
msdata:rowOrder
="
9
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
锦州
</
TrainStation
>
<
ArriveTime
>
04:06:00
</
ArriveTime
>
<
StartTime
>
04:15:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo11
"
msdata:rowOrder
="
10
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
葫芦岛
</
TrainStation
>
<
ArriveTime
>
04:48:00
</
ArriveTime
>
<
StartTime
>
04:51:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo12
"
msdata:rowOrder
="
11
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
山海关
</
TrainStation
>
<
ArriveTime
>
06:19:00
</
ArriveTime
>
<
StartTime
>
06:25:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo13
"
msdata:rowOrder
="
12
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
北戴河
</
TrainStation
>
<
ArriveTime
>
06:54:00
</
ArriveTime
>
<
StartTime
>
06:57:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo14
"
msdata:rowOrder
="
13
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
昌黎
</
TrainStation
>
<
ArriveTime
>
07:18:00
</
ArriveTime
>
<
StartTime
>
07:31:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo15
"
msdata:rowOrder
="
14
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
滦县
</
TrainStation
>
<
ArriveTime
>
07:58:00
</
ArriveTime
>
<
StartTime
>
08:02:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo16
"
msdata:rowOrder
="
15
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
唐山
</
TrainStation
>
<
ArriveTime
>
08:53:00
</
ArriveTime
>
<
StartTime
>
08:59:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo17
"
msdata:rowOrder
="
16
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
芦台
</
TrainStation
>
<
ArriveTime
>
09:29:00
</
ArriveTime
>
<
StartTime
>
09:34:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo18
"
msdata:rowOrder
="
17
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
天津
</
TrainStation
>
<
ArriveTime
>
10:35:00
</
ArriveTime
>
<
StartTime
>
10:54:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo19
"
msdata:rowOrder
="
18
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
霸州
</
TrainStation
>
<
ArriveTime
>
12:21:00
</
ArriveTime
>
<
StartTime
>
12:25:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo20
"
msdata:rowOrder
="
19
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
任丘
</
TrainStation
>
<
ArriveTime
>
13:00:00
</
ArriveTime
>
<
StartTime
>
13:03:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo21
"
msdata:rowOrder
="
20
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
深州
</
TrainStation
>
<
ArriveTime
>
14:52:00
</
ArriveTime
>
<
StartTime
>
14:56:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo22
"
msdata:rowOrder
="
21
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
衡水
</
TrainStation
>
<
ArriveTime
>
15:26:00
</
ArriveTime
>
<
StartTime
>
15:32:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo23
"
msdata:rowOrder
="
22
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
枣强
</
TrainStation
>
<
ArriveTime
>
15:56:00
</
ArriveTime
>
<
StartTime
>
16:00:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo24
"
msdata:rowOrder
="
23
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
清河城
</
TrainStation
>
<
ArriveTime
>
16:44:00
</
ArriveTime
>
<
StartTime
>
16:49:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo25
"
msdata:rowOrder
="
24
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
临清
</
TrainStation
>
<
ArriveTime
>
17:20:00
</
ArriveTime
>
<
StartTime
>
17:25:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo26
"
msdata:rowOrder
="
25
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
聊城
</
TrainStation
>
<
ArriveTime
>
18:00:00
</
ArriveTime
>
<
StartTime
>
18:03:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo27
"
msdata:rowOrder
="
26
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
梁山
</
TrainStation
>
<
ArriveTime
>
18:49:00
</
ArriveTime
>
<
StartTime
>
19:03:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo28
"
msdata:rowOrder
="
27
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
菏泽
</
TrainStation
>
<
ArriveTime
>
19:50:00
</
ArriveTime
>
<
StartTime
>
19:52:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo29
"
msdata:rowOrder
="
28
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
商丘南
</
TrainStation
>
<
ArriveTime
>
21:01:00
</
ArriveTime
>
<
StartTime
>
21:03:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo30
"
msdata:rowOrder
="
29
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
亳州
</
TrainStation
>
<
ArriveTime
>
21:45:00
</
ArriveTime
>
<
StartTime
>
21:48:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo31
"
msdata:rowOrder
="
30
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
阜阳
</
TrainStation
>
<
ArriveTime
>
23:00:00
</
ArriveTime
>
<
StartTime
>
23:10:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo32
"
msdata:rowOrder
="
31
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
淮滨
</
TrainStation
>
<
ArriveTime
>
00:14:00
</
ArriveTime
>
<
StartTime
>
00:18:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo33
"
msdata:rowOrder
="
32
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
潢川
</
TrainStation
>
<
ArriveTime
>
01:03:00
</
ArriveTime
>
<
StartTime
>
01:05:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo34
"
msdata:rowOrder
="
33
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
麻城
</
TrainStation
>
<
ArriveTime
>
02:34:00
</
ArriveTime
>
<
StartTime
>
02:38:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo35
"
msdata:rowOrder
="
34
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
黄州
</
TrainStation
>
<
ArriveTime
>
03:27:00
</
ArriveTime
>
<
StartTime
>
03:29:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo36
"
msdata:rowOrder
="
35
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
浠水
</
TrainStation
>
<
ArriveTime
>
03:56:00
</
ArriveTime
>
<
StartTime
>
03:58:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo37
"
msdata:rowOrder
="
36
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
蕲春
</
TrainStation
>
<
ArriveTime
>
04:22:00
</
ArriveTime
>
<
StartTime
>
04:24:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo38
"
msdata:rowOrder
="
37
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
九江
</
TrainStation
>
<
ArriveTime
>
05:36:00
</
ArriveTime
>
<
StartTime
>
05:49:00
</
StartTime
>
</
TrainDetailInfo
>
-
<
TrainDetailInfo
diffgr:id
="
TrainDetailInfo39
"
msdata:rowOrder
="
38
"
diffgr:hasChanges
="
inserted
"
>
<
TrainStation
>
南昌
</
TrainStation
>
<
ArriveTime
>
07:55:00
</
ArriveTime
>
</
TrainDetailInfo
>
</
getDetailInfo
>
</
diffgr:diffgram
>
</
DataSet
>
——————————————————————————————————
对于这个xml数据我们需要
<TrainStation>九江</TrainStation>
<
ArriveTime
>
05:36:00
</
ArriveTime
>
<
StartTime
>
05:49:00
</
StartTime
>
当我们输入车次时候我们需要的是这个数据在获取这个数据之前我们需要设计个类
public class TrainDetailInfo
{
public string TrainStation { get; set; }
public string ArriveTime { get; set; }
public string StartTime { get; set; }
public string KM { get; set; }
}
好了写到这里 开始进入最重要的地方哦 集中精力哦!
调用这个web服务我们需要建立一个实例
public TrainCode()
{
InitializeComponent();
//建立一个web服务的client实例
ServiceReference1.TrainTimeWebServiceSoapClient client= new ServiceReference1.TrainTimeWebServiceSoapClient();
//处理获取信息完成的事件
client.getDetailInfoByTrainCodeCompleted += new EventHandler<ServiceReference1.getDetailInfoByTrainCodeCompletedEventArgs>(client_getDetailInfoByTrainCodeCompleted);
}
void client_getDetailInfoByTrainCodeCompleted(object sender, ServiceReference1.getDetailInfoByTrainCodeCompletedEventArgs e)
{
//throw new NotImplementedException();
if (e.Result==null)
{
MessageBox.Show("你查询的车次不存在!");
return;
}
try
{
//e.result为反回来的xml数据下面这段代码就是解析xml数据的核心代码 属于linq 查询
//需要添加using System.Xml.Linq;using System.Linq;引用
//
var list = e.Result.Nodes[0];
var train = from table in list.Descendants("TrainDetailInfo")//类初始化器
select new TrainDetailInfo
{
TrainStation = (string)table.Element("TrainStation"),
StartTime = (string)table.Element("StartTime"),
ArriveTime =(string) table.Element("ArriveTime").Value,
KM = (string)table.Element("KM")
};
//这里是绑定数据
this.listTrain.ItemsSource = train;
}
catch
{
MessageBox.Show("没有找到你要的信息!");
}
}
private void AppBar_search_Click_Click(object sender, System.EventArgs e)
{
client.getDetailInfoByTrainCodeAsync(textBox1.Text,null);
}
好了大家都写完了没 出现很多bug?
希望在家在遇到真么不懂的直接baidu和google吧 !
有什么不懂的地方大家一起讨论哦!