“消息”子页设计完成了,又该怎么去获取“消息”的天气和当前地点等信息数据。我们需在界面的Load事件中调用GPS的GetGps方法,这里楼楼设置了GPS的超时时间,超时时间是可以不设置的,因为默认的超时时间是6000毫秒
'''获取当前日期
lbldatetime.Text = System.DateTime.Today.ToString("MM月dd日 dddd", New System.Globalization.CultureInfo("zh-CN"))
'''GPS超时时间
Gps1.TimeOut = 7000 '超时时间
'''调用GPS
Gps1.GetGps()
获取GPS数据则需要通过GPS的GotLocation事件,这里我们需要获取天气所以要调用天气的插件
public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
}
private void Gps1_GotLocation(object sender, GPSData e)
{
try {
if (string.IsNullOrEmpty(e.ErrorInfo)) {
if (e.Longitude.Equals(0) & e.Latitude.Equals(0)) {
this.LblGps.Text = "定位中...";
} else {
this.LblGps.Text = e.Location;
RemoteCertificateValidationCallback re = new RemoteCertificateValidationCallback(ValidateServerCertificate);
ServicePointManager.ServerCertificateValidationCallback = re;
string Longitude = e.Longitude.ToString;
//经度
string Latitude = e.Latitude.ToString;
//纬度
WebClient WebServer = new WebClient();
string str = "http://api.map.baidu.com/telematics/v3/weather?location=" + Longitude + "," + Latitude + "&output=xml&ak=h9unLzxMnPSE9hoDTvGUw9YB&mcode=100";
HttpWebRequest request = HttpWebRequest.Create(str);
HttpWebResponse response = request.GetResponse();
StreamReader sr2 = new StreamReader(response.GetResponseStream, Encoding.UTF8);
string text2 = sr2.ReadToEnd();
Xml.XmlDocument doc = new Xml.XmlDocument();
doc.LoadXml(text2);
Xml.XmlNode weather1 = doc.SelectSingleNode("CityWeatherResponse").Item("results").Item("weather_data").Item("weather");
string str1 = weather1.FirstChild().Value.ToString;
Xml.XmlNode temper1 = doc.SelectSingleNode("CityWeatherResponse").Item("results").Item("weather_data").Item("temperature");
string str2 = temper1.FirstChild().Value.ToString;
Xml.XmlNode wind1 = doc.SelectSingleNode("CityWeatherResponse").Item("results").Item("weather_data").Item("wind");
string str3 = wind1.FirstChild().Value.ToString;
Xml.XmlNode city1 = doc.SelectSingleNode("CityWeatherResponse").Item("results").Item("currentCity");
string str4 = city1.FirstChild().Value.ToString;
this.lblweather.Text = str2 + " " + str1 + " ";
}
}
} catch (Exception ex) {
this.LblGps.Text = "定位中...";
}
}
在“消息”界面Load事件中加入Gridview数据绑定代码, GetMessage()是楼楼自定义的一个方法,用来获取的消息数据。
DataTable table = GetMessage();//获取消息数据
if (table.Rows.Count > 0) {
GridView1.DataSource = table;
GridView1.DataBind();
} else {
this.GridView1.Rows.Clear();
}
刚刚楼楼说过主页间子页面的导航是用toolbar按钮来实现的。在“消息”,“报销”,“其他”子页上都设置4个toolbar按钮。
因为“消息”子页是主页的第一个界面,所以我们在“消息”界面,定义一个 toolbar子页面切换方法ProcessFormToolBarName,当点击toolbar上的“消息"按钮时,会在当前界面“消息”子页,当点击其他按钮会跳到相应的界面,在其他的子页面需要定义一个公共变量ToolBarName传toolbar的name值。
///
/// toolbar子页面切换方法
///
///
///
private void ProcessMenuFormToolBarName(string toolbarname)
{
try {
switch (toolbarname) {
case RB.Name:
frmRBMune2 frmRBMain = new frmRBMune2();
this.Redirect(frmRBMain, () => { ProcessMenuFormToolBarName(frmRBMain.ToolBarName); });
break;
case Other.Name:
frmMune frmMune = new frmMune();
this.Redirect(frmMune, () => { ProcessMenuFormToolBarName(frmMune.ToolBarName); });
break;
case tExit.Name:
MessageBox.Show("是否确定退出系统?", MessageBoxButtons.YesNo, (Object s, MessageBoxHandlerArgs args) =>
{
if (args .Result == Smobiler.Core.ShowResult.Yes)
{
this.Close();
}
});
}
} catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
然后再在“消息”子页界面调用ToolbarItemClick事件便可;
///
/// toolbar
///
///
///
///
private void frmMessage_ToolbarItemClick(object sender, ToolbarClickEventArgs e)
{
try {
ProcessMenuFormToolBarName(e.Name);
} catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
在其他子页界面ToolbarItemClick判断不等于当前界面的toolbar按钮时,则将点击toolbar按钮的name值赋值给变量ToolBarName,并关闭当前界面。
if (e.Name != Other.Name) {
ToolBarName = e.Name;
this.Close();
}