【WPF】【火车站点信息查询】
全文涉及到的是C#和XAML
如果这两门语言并非你喜欢的语言,那可以关闭本网页了
- 本文介绍的是什么?
- 一个火车站点信息查询软件
- 本文涉及到的WPF基本知识
- Task
- async await
- WebClient
- 数据绑定
这基本知识其他blog会有这更详细的讲解,本文将不会详细介绍这些内容
- 实现的基本原理
- 通过webclient 获取 指定网页静态资源
- 分析静态资源,并提取火车的站点信息
- 编译环境和平台
- VS2012
- .NET4.5
先通过几张截图看看本文介绍的应用是什么


界面做的不是太好,但基本功能都已实现
界面的实现
- 界面中包含了一个静态资源 DataTemplate UITraninID
- 作为显示所有火车ID的List的ItemTemplat
- UIMainGrid 用于显示某火车的详细站点信息
- 包含一个Textblock和一个DataGrid,分别显示ID和详细站点
<Window x:Class="Train.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="450" Width="525" WindowStartupLocation="CenterScreen">
<Window.Resources>
<DataTemplate x:Key="UITrainID">
<TextBlock Text="{Binding ID}" Foreground="Black" FontSize="17" Width="70" TextAlignment="Center" />
</DataTemplate>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Background="Black">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="140"/>
</Grid.ColumnDefinitions>
<TextBlock Text="欢迎" Foreground="White" FontSize="20" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<Button x:Name="UIOpenFile" BorderThickness="0" Foreground="White" Background="Transparent" Content="打开文件" Grid.Column="1" FontSize="20" Click="UIOpenFile_Click" />
<Button x:Name="UISave" BorderThickness="0" Foreground="White" Background="Transparent" Content="保存" Grid.Column="2" FontSize="20" Click="UISave_Click" />
<Button x:Name="UIAbout" BorderThickness="0" Foreground="White" Background="Transparent" Content="关于本应用" Grid.Column="3" FontSize="20" Click="UIAbout_Click" />
</Grid>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Background="LightSkyBlue">
<TextBlock Text="列车ID" FontSize="19" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
<ListView
x:Name="UIlist"
ItemTemplate="{StaticResource ResourceKey=UITrainID}"
BorderThickness="0"
Grid.Row="1" SelectionChanged="UIlist_SelectionChanged"/>
</Grid>
<Grid x:Name="UIMainGrid" Grid.Column="1">
<Border BorderThickness="1,0,0,0" BorderBrush="Black" />
<Grid Margin="30,15,30,50">
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Background="LightGreen">
<TextBlock Text="{Binding ID}" FontSize="20" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="20,0,0,0" />
</Grid>
<Grid Background="AliceBlue" Grid.Row="1">
<DataGrid IsReadOnly="True" ItemsSource="{Binding All_station}" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
</Grid>
</Grid>
<Grid Grid.Column="1">
<ProgressBar x:Name="UIPro" Value="0" Background="Salmon" Height="40" Margin="86,0,0,0" VerticalAlignment="Bottom"/>
<TextBlock x:Name="UItext" TextWrapping="Wrap" Text="100" VerticalAlignment="Top" FontSize="17" HorizontalAlignment="Left" Margin="100,33,0,0" />
<TextBlock Foreground="Green" HorizontalAlignment="Left" Margin="25,0,0,10" TextWrapping="Wrap" Text="进度" VerticalAlignment="Bottom" FontSize="17" FontWeight="Bold"/>
</Grid>
</Grid>
</Grid>
</Window>
下面设计详细
这是下文将会用到的几个静态资源
private static string send = "</div>";//站点信息结束标示符
private static string sbegin = "result-data";//站点信息开始标示符
private static string SiteUrl = "http://piao.huoche.com/checi/{0}/";//URL模板
private static string surl;
public static List<TrainInfo> AllTrain = new List<TrainInfo>(); //所有信息
1.获取指定网页静态资源
public static List<station> GetWebData()
{
WebClient myWebClient = new WebClient();
//skip certificate error
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
try
{
// stream get html_sourcefile
Stream myStream = myWebClient.OpenRead(surl);
StreamReader sr = new StreamReader(myStream, System.Text.Encoding.GetEncoding("utf-8"));
//get data
var result = PickInfo(sr.ReadToEnd());
//close stream
myStream.Close();
return result;
}
catch { return null; }
}
通过OpenRead方法获取 指定url 的数据流
实体化StreamReader类,并调用ReadToEnd()方法,获取静态string资源
并调静态资源分析函数
返回指定网页的所有站点的一个 List<station>
2.分析网页源码
public static List<station> PickInfo(string s)
{
List<station> allstation = new List<station>();
int index_begin = s.IndexOf(sbegin);
int index_end = s.IndexOf(send, index_begin);
)
.Split('<', '>');
, value = ;
; i < stemp.Length; i++)
{
stemp[i] = stemp[i].Trim();
try
{
value = int.Parse(stemp[i]);
}
catch
{
continue;
}
if (value != n) continue;
//while (stemp[i].Contains("td") || !ISChinese(stemp[i])) i++;
i+=;
station astation = new station
{
Name = stemp[i],
Index = n
};
i+=;
astation.ArriveTime = stemp[i];
i+=;
astation.LeaveTime = stemp[i];
i+=;
astation.TimeSum = stemp[i];
i += ;
allstation.Add(astation);
n++;
}
return allstation;
}
简单的字符串处理,代码写的并不是太好。。
3.我已经有了一个保存所有车次ID的文本,如何获取并显示所有火车信息
读取文本信息,对于每一ID,实体化一个 TrainInfo类,并加入到AllTrainList里面
TrainInfo 包含两个属性
- string ID
- list<station> all_station
通过调用异步方法GETDATA()
[网络不稳定因素可能导致调用该方法时占用大量时间,所以采用异步实现]
根据ID,生成一个URL连接,并new 一个task->GetWebData ,去获取该页面的数据
public static async Task<List<station>> GetData(string id)
{
Task<List<station>> task = new Task<List<station>>(GetWebData);
surl = string.Format(SiteUrl,id);
task.Start();
return await task;
}
4.UI显示
UI主要有两个控件用于显示数据,分别是
- ListView,显示查询的所有火车的ID
- DataGrid,显示某个ID的所有站点信息
为什么不是每一个TrainInfo都对应一个DataGrid
在大量查询时,如果每个TrainInfo都对应一个DataGrid,将会占用大量内存
还有一个progressbar 用于显示当前数据的获取状态
具体实现如下
private async void UIOpenFile_Click(object sender, RoutedEventArgs e)
{
if (!Global.fileopen()) return;
//showpro();
IdList.Clear();
UIPro.Maximum = Global.AllTrain.Count;
UIPro.Value = ;
foreach (var x in Global.AllTrain)
{
UItext.Text = string.Format(sGetInfo, x.ID) + "\n" + swait;
x.All_station = await Global.GetData(x.ID);
IdList.Add(new UITrainTextBlock { ID = x.ID });
UIPro.Value++;
}
UItext.Text = "";
//showdata();
}
源工程下载:
http://files.cnblogs.com/lightz/Train.zip
【WPF】【火车站点信息查询】的更多相关文章
- java实现根据起点终点和日期查询去哪儿网的火车车次和火车站点信息
本文章为原创文章,转载请注明,欢迎评论和改正. 一,分析 之前所用的直接通过HTML中的元素值来爬取一些网页上的数据,但是一些比较敏感的数据,很多正规网站都是通过json数据存储,这些数据通过HTML ...
- 火车车次查询-余票查询--Api接口
1.来自12306的火车车次数据 使用12306网站的接口,查询余票.此接口采集自 这里. 全国火车站代号字典,下载 . 火车票余票查询 http://dynamic.12306.cn/otsquer ...
- jQuery Mobile应用之火车票查询
效果图: 在CMD中输入如下代码 corsproxy (前提是有node.js环境,并先安装corsproxy) html: <!DOCTYPE html> <html> &l ...
- 我的Android进阶之旅------>关于调用Webservice查询火车票时刻表的几个接口介绍
今天发现一个可以提供火车票时刻表查询的WebService,先记录下来以后如果写一个火车票时刻表查询的Android App的话就用的着.首先该WebService的的名字是TrainTimeWebS ...
- 最实用的IT类网站及工具大集合
1.聚合数据 大家在开发过程中,可能会用到各种各样的数据,想找一些接口来提供一些数据.比如天气预报查询,火车时刻表查询,彩票查询,身份证查询等等.有了这个接口,直接调用即可.各种各样的API接口满足你 ...
- 干货!IT小伙伴们实用的网站及工具大集合!持续更新!
1.Git 还在担心自己辛辛苦苦写的代码被误删了吗?还在担心自己改错了代码不能挽回吗?还在苦恼于多人开发合作找不到一个好的工具吗?那么用Git就对 了,Git是一个开源的分布式版本控制系统,用以有效. ...
- Android和Linux应用综合对比分析
原文地址:http://www.cnblogs.com/beer/p/3325242.html 免责声明: 当时写完这篇调查报告,给同事看了后,他觉得蛮喜欢,然后想把这篇文章修改一下,然后往期刊上发表 ...
- 最实用的IT类网站及工具大集合[转]
1.聚合数据 大家在开发过程中,可能会用到各种各样的数据,想找一些接口来提供一些数据.比如天气预报查询,火车时刻表查询,彩票查询,身份证查询等等.有了这个接口,直接调用即可.各种各样的API接口满足你 ...
- IOS网络开发(三)
1 飞机航班查询软件 1.1 问题 NSURLConnection是IOS提供的用于处理Http协议的网络请求的类,可以实现同步请求也可以实现异步请求,本案例使用NSURLConnection类实现一 ...
随机推荐
- BAT面经
http://bbs.csdn.net/topics/390734210?page=4 注意评论以及文章原地址
- Codeforces 672
题目链接:http://codeforces.com/contest/672/problem A. Summer Camp(打表) 题意:123456789...一串字符串,问第n个是什么数字. 塞一 ...
- Android Studio修改包名和applicationId的方法
背景: 如果新做的项目跟以前做的某一个项目十分相似,那么一个简单的方法就是把原来项目拷贝一份,然后修改代码,但是这样包名还是原来项目的包名,还有如果想在同一台手机上同时安装新做的app和原来的app会 ...
- Redis VS Memcached
1. Redis & Memecached比较 内存管理 持久化 数据类型 客户端支持 并发性能 Memcached 预分配的内存池的方式 不支持持久化 支持简单的key-value存储 ...
- Linux 下查看文件字符编码和转换编码
Linux 下查看文件字符编码和转换编码 如果你需要在Linux中操作windows下的文件,那么你可能会经常遇到文件编码转换的问题.Windows中默认的文件格式是GBK(gb2312),而Linu ...
- fil_system_struct
/** The tablespace memory cache */ typedef struct fil_system_struct fil_system_t; /** The tablespace ...
- UVa 11054 Wine trading in Gergovia【贪心】
题意:给出n个等距离的村庄,每个村庄要么买酒,要么卖酒,买酒和卖酒的总量相等, 把k个单位的酒从一个村庄运送到相邻的村庄,需要耗费k个单位劳动力,问怎样运送酒使得耗费的劳动力最少 买 卖 ...
- UVALive 2238 Fixed Partition Memory Management(二分完美匹配)
题意:计算机中有一些固定大小的内存,内存越大,处理速度越快.对于一个程序,加入不同的内存空间,处理所需时间不同.现给出m个内存空间,n个程序,对于每个程序程序,有k组数据(s,t),分别表示当程序 i ...
- 【C#学习笔记】打开新进程
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 流媒体相关知识介绍 及其 RTP 应用
一.流媒体简介 随着Internet的日益普及,在网络上传输的数据已经不再局限于文字和图形,而是逐渐向声音和视频等多媒体格式过渡.目前在网络上传输音频/视频(Audio/Video,简称A/V)等多媒 ...