今天在論壇上看到有朋友問如何在Windows Mobile的控制台應用中使用Microsoft.WindowsCE.Forms.Notification這個類。恰好自己也沒有研究過這個類的使用,所以就打算試試。
Google了一把,排在前面的就是MSDN上的協助,居然還是中文的,真是太難得了。這篇技術資源文章《Notification類》講解如何?用於顯示和響應使用者通知的 Windows CE 功能。需要主意的是,在備忘中有這麼一段話,“此類提供 Windows CE 通知功能的託管實現。只有 Pocket PC 上支援此類。”顯然,我們要在WM6上調試的話,要選擇professional的版本才行。下面就一步步來進行講解吧。
1. 在Visual Studio 2005中,在C#的smart device下,選擇Windows Mobile 6 Professional,在右邊選中console application,即控制台應用程式。選定好工程的名稱和路徑等之後,點擊確定。如1所示:
圖1:建立工程頁面
2. 將《Notification類》中C#下的代碼全部複製,拷貝到工程的program.cs中,進行編譯。
3. 發現編譯出錯,提示是“沒有找到System.Windows.Forms、System.Drawing和Microsoft.WindowsCE.Forms”之類的引用。解決方案:在工程的Refence中點擊右鍵,添加相關的引用,然後編譯,就可以通過了,如2所示:
圖2:添加引用介面
4. 選擇Windows Mobile 6 Professional Emulator進行調試,如3所示:
圖3:選擇模擬器進行調試
5. 程式下載以後運行,發現程式立即拋出一個異常,“value does not fall within the expected range”,如4所示:
圖4:程式異常介面
6. 這個時候,就得單步調試來尋找問題了,覺得是初始化的時候出了問題。果然,在ConfigNotification函數中,執行到擷取notification的Icon時,程式拋出了異常,該函數的代碼如下:
Code
1 private void ConfigNotification()
2 {
3 // Create a Notification.
4 notification1 = new Microsoft.WindowsCE.Forms.Notification();
5
6 try
7 {
8 // Provide an icon for the notification to appear in the title bar when dismissed.
9 // Assumes an icon file is compiled with the assembly as an embedded resource.
10 Assembly asm = Assembly.GetExecutingAssembly();
11 //notification1.Icon = new Icon(asm.GetManifestResourceStream("notify.ico"), 16, 16);
12
13 notification1.Caption = "Notification scenario - data download";
14
15 // If notification is urgent, set to true.
16 notification1.Critical = false;
17
18 // Create the text for the notification.
19 // Use a StringBuilder for better performance.
20 StringBuilder HTMLString = new StringBuilder();
21
22 HTMLString.Append("<html><body>");
23 HTMLString.Append("<font color=\"#0000FF\"><b>Data ready to download</b></font>");
24 HTMLString.Append(" <a href=\"settings\">Settings</a>");
25 HTMLString.Append("<br><form method=\"GET\" action=notify>");
26 HTMLString.Append("<SELECT NAME=\"lstbx\">");
27 HTMLString.Append("<OPTION VALUE=\"0\">Start now</OPTION><OPTION VALUE=\"1\">In 1 hr</OPTION>");
28 HTMLString.Append("<OPTION VALUE=\"2\">In 2 hrs</OPTION><OPTION VALUE=\"3\">In 3 hrs</OPTION>");
29 HTMLString.Append("<OPTION VALUE=\"4\">In 4 hrs</OPTION></SELECT>");
30 HTMLString.Append("<input type=checkbox name=chkbx>Notify completion");
31 HTMLString.Append("<br><input type='submit'>");
32 HTMLString.Append("<input type=button name='cmd:2' value='Postpone'>");
33 HTMLString.Append("</body></html>");
34
35 // Set the Text property to the HTML string.
36 notification1.Text = HTMLString.ToString();
37
38 // Add event handlers.
39
40 notification1.BalloonChanged += new BalloonChangedEventHandler(OnBalloonChanged);
41 notification1.ResponseSubmitted += new ResponseSubmittedEventHandler(OnResponseSubmitted);
42
43 }
44
45 catch (Exception ex)
46 {
47 MessageBox.Show(ex.Message);
48 }
49
50
51 }
52
其實程式中已經有注釋了,提醒使用者說,假設該icon已經作為內嵌資源被編譯。
找到問題了,最簡單的處理方法,不就是icon嘛,我不看也行,直接屏蔽掉這句吧,先看程式啟動並執行效果如何。單擊介面的Notify按鈕,彈出來使用者佈建介面,如5所示:
圖5:Notify程式運行介面
參考連結:
MSDN:Notification類