在Windows Mobile的控制台应用中使用Notification

今天在论坛上看到有朋友问如何在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所示:

在Windows Mobile的控制台应用中使用Notification

1:新建工程页面

      2. 将《Notification》中C#下的代码全部复制,拷贝到工程的program.cs中,进行编译。

      3. 发现编译出错,提示是“没有找到System.Windows.FormsSystem.DrawingMicrosoft.WindowsCE.Forms”之类的引用。解决方法:在工程的Refence中点击右键,添加相关的引用,然后编译,就可以通过了,如下图2所示:

在Windows Mobile的控制台应用中使用Notification

2:添加引用界面

      4. 选择Windows Mobile 6 Professional Emulator进行调试,如下图3所示:

在Windows Mobile的控制台应用中使用Notification

3:选择模拟器进行调试

      5. 程序下载以后运行,发现程序立即抛出一个异常,“value does not fall within the expected range”,如下图4所示:

在Windows Mobile的控制台应用中使用Notification

4:程序异常界面

      6. 这个时候,就得单步调试来寻找问题了,觉得是初始化的时候出了问题。果然,在ConfigNotification函数中,执行到获取notificationIcon时,程序抛出了异常,该函数的代码如下:

在Windows Mobile的控制台应用中使用Notification在Windows Mobile的控制台应用中使用NotificationCode
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt 1在Windows Mobile的控制台应用中使用Notification        private void ConfigNotification()
 2在Windows Mobile的控制台应用中使用Notification在Windows Mobile的控制台应用中使用Notification        在Windows Mobile的控制台应用中使用Notification{
 3在Windows Mobile的控制台应用中使用Notification            // Create a Notification.
 4在Windows Mobile的控制台应用中使用Notification            notification1 = new Microsoft.WindowsCE.Forms.Notification();
 5在Windows Mobile的控制台应用中使用Notification
 6在Windows Mobile的控制台应用中使用Notification            try
 7在Windows Mobile的控制台应用中使用Notification在Windows Mobile的控制台应用中使用Notification            在Windows Mobile的控制台应用中使用Notification{
 8在Windows Mobile的控制台应用中使用Notification                // Provide an icon for the notification to appear in the title bar when dismissed.
 9在Windows Mobile的控制台应用中使用Notification                // Assumes an icon file is compiled with the assembly as an embedded resource.
10在Windows Mobile的控制台应用中使用Notification                Assembly asm = Assembly.GetExecutingAssembly();
11在Windows Mobile的控制台应用中使用Notification                //notification1.Icon = new Icon(asm.GetManifestResourceStream("notify.ico"), 16, 16);
12在Windows Mobile的控制台应用中使用Notification
13在Windows Mobile的控制台应用中使用Notification                notification1.Caption = "Notification scenario - data download";
14在Windows Mobile的控制台应用中使用Notification
15在Windows Mobile的控制台应用中使用Notification                // If notification is urgent, set to true.
16在Windows Mobile的控制台应用中使用Notification                notification1.Critical = false;
17在Windows Mobile的控制台应用中使用Notification
18在Windows Mobile的控制台应用中使用Notification                // Create the text for the notification.
19在Windows Mobile的控制台应用中使用Notification                // Use a StringBuilder for better performance.
20在Windows Mobile的控制台应用中使用Notification                StringBuilder HTMLString = new StringBuilder();
21在Windows Mobile的控制台应用中使用Notification
22在Windows Mobile的控制台应用中使用Notification                HTMLString.Append("");
23在Windows Mobile的控制台应用中使用Notification                HTMLString.Append(""#0000FF\">Data ready to download");
24在Windows Mobile的控制台应用中使用Notification                HTMLString.Append("    "settings\">Settings");
25在Windows Mobile的控制台应用中使用Notification                HTMLString.Append("
"GET\" action=notify>");
26在Windows Mobile的控制台应用中使用Notification                HTMLString.Append(""lstbx\">");
27在Windows Mobile的控制台应用中使用Notification                HTMLString.Append(""0\">Start now"1\">In 1 hr");
28在Windows Mobile的控制台应用中使用Notification                HTMLString.Append(""2\">In 2 hrs"3\">In 3 hrs");
29在Windows Mobile的控制台应用中使用Notification                HTMLString.Append(""4\">In 4 hrs
");
30在Windows Mobile的控制台应用中使用Notification                HTMLString.Append("Notify completion");
31在Windows Mobile的控制台应用中使用Notification                HTMLString.Append("
");
32在Windows Mobile的控制台应用中使用Notification                HTMLString.Append("");
33在Windows Mobile的控制台应用中使用Notification                HTMLString.Append("");
34在Windows Mobile的控制台应用中使用Notification
35在Windows Mobile的控制台应用中使用Notification                // Set the Text property to the HTML string.
36在Windows Mobile的控制台应用中使用Notification                notification1.Text = HTMLString.ToString();
37在Windows Mobile的控制台应用中使用Notification
38在Windows Mobile的控制台应用中使用Notification                // Add event handlers.
39在Windows Mobile的控制台应用中使用Notification
40在Windows Mobile的控制台应用中使用Notification                notification1.BalloonChanged += new BalloonChangedEventHandler(OnBalloonChanged);
41在Windows Mobile的控制台应用中使用Notification                notification1.ResponseSubmitted += new ResponseSubmittedEventHandler(OnResponseSubmitted);
42在Windows Mobile的控制台应用中使用Notification
43在Windows Mobile的控制台应用中使用Notification            }

44在Windows Mobile的控制台应用中使用Notification
45在Windows Mobile的控制台应用中使用Notification            catch (Exception ex)
46在Windows Mobile的控制台应用中使用Notification在Windows Mobile的控制台应用中使用Notification            在Windows Mobile的控制台应用中使用Notification{
47在Windows Mobile的控制台应用中使用Notification                MessageBox.Show(ex.Message);
48在Windows Mobile的控制台应用中使用Notification            }

49在Windows Mobile的控制台应用中使用Notification
50在Windows Mobile的控制台应用中使用Notification
51在Windows Mobile的控制台应用中使用Notification        }

52在Windows Mobile的控制台应用中使用Notification

 

    其实程序中已经有注释了,提醒用户说,假设该icon已经作为嵌入的资源被编译。

    找到问题了,最简单的处理方法,不就是icon嘛,我不看也行,直接屏蔽掉这句吧,先看程序运行的效果如何。单击界面的Notify按钮,弹出来用户设置界面,如下图5所示:

在Windows Mobile的控制台应用中使用Notification

5Notify程序运行界面

参考链接:

MSDNNotification

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/14766526/viewspace-558128/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/14766526/viewspace-558128/