基於Windows Mobile 5.0的掌上天氣預報設計
概述:描述一個基於Windows mobile 5.0的天氣預報程式設計過程;
開發工具:Visual studio 2005(C#/)Windows Mobile 5.0 Pocket PC SDK/WM6 模擬器;
試用機型:多普達 D600
關鍵字:.NET CF,PPC, WebService,Windows mobile, 天氣預報
下載安裝程式:PocketWeatherInstall.cab
下載 原始碼:PocketWeather.rar
總的來說,使用.NET Compact Framework 設計程式還是比較容易入手的,本文就在設計過程中碰到的幾個問題做一個簡要說明。
1. 天氣介面
採用一個網上免費的Web服務,來源www.WebXml.com.cn,使用非常方便,調用getWeatherbyCityName返回一個string數組,包含相關天氣資訊。
string city = this.lblCurrentCity.Text.Trim();
PocketWeather.cn.com.webxml.www.WeatherWebService Weather =
new PocketWeather.cn.com.webxml.www.WeatherWebService();
string[] info = Weather.getWeatherbyCityName(city);
2. 擷取程式運行路徑
PPC擷取當前程式路徑還比較麻煩:
public static string GetApplicationDirectory()
{
return System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
}
3. 儲存使用者佈建
將使用者佈建資訊儲存在一個DataSet中,然後儲存為本地XML檔案:
DataSet ds = new DataSet("LastWeather");
DataTable tabLast = new DataTable("Weather");
tabLast.Columns.Add("KeyStr", typeof(string));
tabLast.Columns.Add("Content", typeof(string));
DataRow row = tabLast.NewRow();
row["KeyStr"] = "lblTodayWeather";
row["Content"] = this.lblTodayWeather.Text;
tabLast.Rows.Add(row);
row = tabLast.NewRow();
row["KeyStr"] = "LastModifyTime";
row["Content"] = SystemConfig.LastModifyTime;
tabLast.Rows.Add(row);
ds.Tables.Add(tabLast);
ds.WriteXml(SystemConfig.GetApplicationDirectory() + """Last.xml");
讀取時從XML檔案擷取DataSet,然後再讀取資料。
DataSet ds = new DataSet();
ds.ReadXml(SystemConfig.GetApplicationDirectory() + """Last.xml");
DataTable tabSetting = ds.Tables["Weather"];
DataRow[] rows = tabSetting.Select("KeyStr='lblTodayWeather'");
if (rows.Length != 0)
{
this.lblTodayWeather.Text = rows[0]["Content"].ToString();
}
rows = tabSetting.Select("KeyStr='LastModifyTime'");
if (rows.Length != 0)
{
SystemConfig.LastModifyTime = rows[0]["Content"].ToString();
}
4.IME面板:
當彈出IME面板是會擋住一些使用者控制項,感覺很不好,處理辦法就是將控制項放在一個Panel中,設定Panel的AutoScroll屬性為True,在面板狀態改變時同時改變Panel的尺寸。
private Microsoft.WindowsCE.Forms.InputPanel m_inp
= new Microsoft.WindowsCE.Forms.InputPanel();
public FormOpetion()
{
InitializeComponent();
this.m_inp.EnabledChanged += new EventHandler(m_inp_EnabledChanged);
}
void m_inp_EnabledChanged(object sender, EventArgs e)
{
m_panel.Size = m_inp.VisibleDesktop.Size;
}
5.對話方塊問題:
程式在開啟一個對話方塊時,如果切換到其他程式,然後在系統運行程式列表中會看到兩項記錄,應該避免這種情況;解決辦法就是在程式啟用和停用時修改視窗標題:
private void FormOpetion_Deactivate(object sender, EventArgs e)
{
this.Text = "";
}
private void FormOpetion_Activated(object sender, EventArgs e)
{
this.Text = "設定";
}
6.其他:
還應該考慮在旋轉螢幕時,要重新調整控制項的位置。
介面: