在Windows Mobile的控制台应用中使用Notification
上一篇 / 下一篇 2009-02-27 22:40:22 / 个人分类:windows mobile开发技术
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时,程序抛出了异常,该函数的代码如下:
Ka}mw~7P0 1
private void ConfigNotification()ITPUB个人空间u't/y8^.O|)g[2

{ITPUB个人空间(o,tpDidh3
// Create a Notification.R-k1y Z/MWeu0 4
notification1 = new Microsoft.WindowsCE.Forms.Notification();ITPUB个人空间y'z#]9[5KJms"@nb5
ITPUB个人空间2icZ k.o%K6
trycu`dHx0 7

{f S0^ lH}sM/\0 8
// Provide an icon for the notification to appear in the title bar when dismissed.ITPUB个人空间!d J"X@IwaQE6l/r9
// Assumes an icon file is compiled with the assembly as an embedded resource.ITPUB个人空间mCv)i'}
e10
Assembly asm = Assembly.GetExecutingAssembly();/T$}2^ nL m/`_2Ts/t011
//notification1.Icon = new Icon(asm.GetManifestResourceStream("notify.ico"), 16, 16);m+X6v \DV012
ITPUB个人空间k\i
H3qJ
?t13
notification1.Caption = "Notification scenario - data download";5pJ lz8T014

i v r2f{'HAm015
// If notification is urgent, set to true.r_)A'[0If3LMq016
notification1.Critical = false;ITPUB个人空间 }#mZUl:X17

7H.f.lm!ka2~t b BM018
// Create the text for the notification.5I"|]gZ*];r&~4L_019
// Use a StringBuilder for better performance.ITPUB个人空间B#Y8I!Y$V20
StringBuilder HTMLString = new StringBuilder();ITPUB个人空间/R:QGT3o_21

:t k+v,{g`lc022
HTMLString.Append("<html><body>");ITPUB个人空间u/s'D&HS%qF!Zk23
HTMLString.Append("<font color=\"#0000FF\"><b>Data ready to download</b></font>");ZLqc:V024
HTMLString.Append(" <a href=\"settings\">Settings</a>");Ck;@y v^3y/m)X)T025
HTMLString.Append("<br><form method=\"GET\" action=notify>");ITPUB个人空间"Ze4@[[6IHE,`26
HTMLString.Append("<SELECT NAME=\"lstbx\">");;Hi7pCzy$U027
HTMLString.Append("<OPTION VALUE=\"0\">Start now</OPTION><OPTION VALUE=\"1\">In 1 hr</OPTION>");ITPUB个人空间 d2}(V
T/|28
HTMLString.Append("<OPTION VALUE=\"2\">In 2 hrs</OPTION><OPTION VALUE=\"3\">In 3 hrs</OPTION>");J}Uw+[XiV029
HTMLString.Append("<OPTION VALUE=\"4\">In 4 hrs</OPTION></SELECT>");"{:F2p"j~$A9F030
HTMLString.Append("<input type=checkbox name=chkbx>Notify completion");ITPUB个人空间
R3}+Gi `xtgO31
HTMLString.Append("<br><input type='submit'>");%n~sv%^\'b032
HTMLString.Append("<input type=button name='cmd:2' value='Postpone'>");ITPUB个人空间b'J(Y
xOJ2\1K
Ixi8C6M33
HTMLString.Append("</body></html>");.Re0i OzH?h2x\034

(cUR x$?Dt035
// Set the Text property to the HTML string.ITPUB个人空间$KN+M;U0~c36
notification1.Text = HTMLString.ToString();~LB g2J&h037
ITPUB个人空间'zn*gP"LTa38
// Add event handlers.ITPUB个人空间p4`4RA/T;q#w39
ITPUB个人空间Dvj,aZ-j40
notification1.BalloonChanged += new BalloonChangedEventHandler(OnBalloonChanged);