在用.NET CF开发Smartphone/Pocket PC应用时,我们经常需要给应用程序增加提醒功能,比如在下午两点时提醒用户去开会,或者在手持设备和PC通过ActiveSync连接时提醒用户同步某项重要数据。目前,实现提醒功能主要有以下几种方式:
- Windows CE .NET提供了CeSetUserNotification(在CE 2.11版本及以后又增加了CeSetUserNotificationEx)这一API,可用于创建或修改提醒。该方法适用于Smartphone和Pocket PC。
- Windows CE .NET还提供了另外一条API,SHNotificationAdd,也可以完成提醒的工作。该提醒是以Pocket PC中的气泡方式显示的,在Smartphone上不予处理。
- Windows CE .NET中内置了Pocket Outlook,并提供了POOM (Pocket Outlook Object Model)以便二次开发。该方法适用于Smartphone和Pocket PC。
以上三种方法在开发时,均不同程度地要用到平台调用 (Platform Invoke) 或COM互操作,这对开发人员的技术要求非常高,同时也不利于提高开发速度。因此,适当的封装是非常有必要的。幸运的是,目前OpenNETCF.org (http://www.opennetcf.org) 已经完成了前两项的工作,在由OpenNETCF开发并免费发放的SDF (Smart Device Framework) 中已经把上述前两项API封装为managed API,从而极大地减少了开发人员的二次开发难度。同时,InTheHand公司 (http://www.inthehand.com) 也提供了一款非常棒的组件——Pocket Outlook,用managed code封装了底层的COM互操作,从而使开发人员从细琐的底层细节摆脱出来。不过,InTheHand这款组件只免费提供测试版,如需要在商业环境中使用还需支付费用。
下面我就通过一系列实例来说明上面几种方法的应用。
1. OpenNETCF.Win32.Notify
OpenNetCF.org在SDF中的OpenNETCF.Win32.Notify命名空间下封装了CESetUserNotification(以及相应的一套API),使用后只需要短短几行代码就能实现简单的提醒功能,如下例,可以在Smartphone/ Pocket PC 2003上设置一个1分钟后弹出的提醒对话框(需要增加对OpenNETCF.dll的引用):
UserNotification notification = new UserNotification();
notification.Action = NotificationAction.Dialog;
notification.Title = "我的提醒";
notification.Text = "别忘了锻炼!";
Notify.SetUserNotification("", DateTime.Now.AddMinutes(1), notification);
实现效果如下:
图1: Smartphone上应用OpenNETCF.Win32.Notify的效果
图2: Pocket PC上应用OpenNETCF.Win32.Notify的效果
如果想修改某项提醒,只需调用Notify.GetUserNotificationHandles返回所有当前系统存储着的提醒的句柄(handle),然后根据其中特定的句柄调用Notify.GetUserNotification就可获得相应的提醒对象,调用Notify.SetUserNotification)来修改该提醒。比如下例,就把刚才设置的提醒内容做了更改:
// 获取提醒对象 (UserNotificationInfoHeader类型)
UserNotificationInfoHeader infoHeader =? Notify.GetUserNotification(handle);
// 取出UserNotification部分并做更改
UserNotification notification = infoHeader.UserNotification;
if (notification != null)
{
notification.Text = "别忘了开会!";
Notify.SetUserNotification(handle,"", DateTime.Now.AddSeconds(18), notification);
}
2. OpenNETCF.Notification
以上方法对Smartphone 2003和Pocket PC 2003同样生效。而对于Pocket PC 2003而言,还有另外一种设置提醒的方式——弹出气泡。该方法对应的操作系统API是SHNotificationAdd,在SDF中该API被封装在OpenNETCF.Notification命名空间下。下述代码演示了这种用法(需要增加对OpenNETCF.Notification.dll的引用):
Notification notification = new Notification();
notification.Duration = 10;
notification.Flags = NotificationFlags.ForceMessage;
notification.HTML = "Hello Windows Mobile";
notification.Title = "Hi";
NotificationEngine engine = new NotificationEngine(OpenNETCF.Security.Cryptography.NativeMethods.Guid.NewGuid());
engine.Add(notification);
注意,这里是通过调用NotificationEngine.Add()方法来把一个气泡提醒增加到拖盘(tray)中。
图3: 在Pocket PC上应用NotificationEngine.Add的效果
同样,我们可以通过调用NotificationEngine.Remove来删除某项提醒,NotificationEngine.Update来更新提醒,NotificationEngine.GetData方法来获取提醒内容。它们所封装的系统API分别是SHNotificationRemove、SHNotificationUpdate和SHNotificationGetData。
3. InTheHand.PocketOutlook
通过InTheHand封装好的PocketOutlook dll,我们可以非常方便地利用Pocket Outlook所提供的强大功能。比如下面的例子演示如何通过几行代码来在本机的Pocket Outlook中增加一项约会,你可以利用类似的方法来增加联系人、日程信息等等(需要增加对InTheHand.Interop.dll和InTheHand.PocketOutlook.dll的引用):
using (OutlookApplication oApp = new OutlookApplication())
{
using (Appointment oAptmt = oApp.CreateAppointment())
{
oAptmt.Subject = "提醒";
oAptmt.Body = "别忘了锻炼!";
oAptmt.Start = DateTime.Now.AddHours(1);
oAptmt.BusyStatus = BusyStatus.OutOfOffice;
oAptmt.Save();
}
}
实现效果如下:
图4: 在Smartphone上应用Pocket Outlook增加约会
图5: 在Pocket PC上应用Pocket Outlook增加约会
打印 | 张贴于 2004-09-26 13:17:00 | Tag:暂无标签
留言反馈
手机:13811726940
我的MSN: zlj23852588@hotmail.com
请教怎么可以正常使用!
开发语言 C# .Net 1.1
Dopod 566 Windows Mobile 2003 Second Edition for Smartphone简体中文版
TypeLoadException
无法从程序集
InTheHand.WindowsMobile.Pocke
Version=1.7.50922.0,
Culture=neutral,
PublickKeyToken=EA38CAA273134
加载类型
InTheHand.WindowsMobile.Pocke
为什么呢?~请指教!
using (OutlookApplication oApp = new OutlookApplication())
这种结构就可以了
请问这种结构和直接用OutlookApplication oApp = new OutlookApplication()有什么不同呢
本人初学VS2005内的VC#和SP2003的开发,看了你那篇“在Smartphone/Pocket PC 2003上设置提醒”,就试着去做了,找来了InTheHand的DLL:InTheHand.Interop.dll和InTheHand.PocketOutlook.dll,成功在VC#引用中添加了,也在使用到的C#源文件中加入了
using InTheHand.WindowsMobile.PocketOutlook;
写好代码,程序也正常编译,但是一到SP2003 SE仿真机上运行就提示:
TypeLoadException
无法从程序集
InTheHand.WindowsMobile.Pocke
Version=1.7.50922.0,
Culture=neutral,
PublickKeyToken=EA38CAA273134
加载类型
InTheHand.WindowsMobile.Pocke
但我检查过用到的两个DLL都已经自动复制到仿真机上的和程序同一个位置的目录上了。请问这是什么原因呢?请求大侠的帮忙?
cLaunch
1204
1855
180
2
0
6
1
1
53
1
1
1
1
0
1
1
0
0
1855
8
19
常用程序
1
1
1
1
1
1
1
16
1
73
1
20
0
\Windows\“开始”菜单\程序
15
联系人
\Windows\“开始”菜单\联系人.lnk
0
便笺
\Windows\“开始”菜单\程序\便笺.lnk
0
SIM卡管理
\Windows\“开始”菜单\程序\SIM卡管理器.lnk
0
资源管理器
\Windows\“开始”菜单\资源管理器.lnk
0
日程表
\Windows\“开始”菜单\日历.lnk
0
图片
\Windows\“开始”菜单\程序\图片.lnk
0
任务
\Windows\“开始”菜单\程序\任务.lnk
0
照相机
\Windows\“开始”菜单\程序\相机.lnk
0
计算器
\Windows\“开始”菜单\程序\计算器.lnk
0
查看内存
\windows\ctlpnl.exe
cplmain.cpl,4
0
关闭程序
\Windows\ctlpnl.exe
cplmain.cpl,4,2
0
STK服务
\Windows\“开始”菜单\程序\STK服务.lnk
0
备份
\Windows\“开始”菜单\程序\备份.lnk
0
相册
\Windows\“开始”菜单\程序\相册.lnk
0
SIM卡短信
\Windows\“开始”菜单\程序\SIM卡短信.lnk
0
江西PDA
1
1
1
1
1
1
1
16
1
73
1
20
0
\Windows\Start Menu
12
报汛站雨情
\Storage\PDA\Smart_YQ.exe
0
水情查询
\Storage\PDA\Smart_SQ.exe
0
台风路径
\Storage\PDA\Smart_TF.exe
0
卫星云图
\Storage\PDA\Smart_YT.exe
0
天气预报
\Storage\PDA\Smart_TQ.exe
0
警报接收
\Storage\PDA\Smart_JB.exe
0
防汛法规
\Storage\PDA\Smart_FG.exe
0
防汛通讯录
\Storage\PDA\Smart_TXL.exe
0
防汛专简报
\Storage\PDA\Smart_ZJB.exe
0
水工程资料
\Storage\PDA\Smart_SG.exe
0
相关资料
\Storage\PDA\Smart_ZL.exe
0
使用帮助
\Storage\PDA\Smart_BZ.exe
0
0
1
1
1
1
1
1
16
1
70
1
14
0
0
商务娱乐
1
1
1
1
1
1
1
16
1
73
1
20
0
\Windows\“开始”菜单
12
Pocket Word
\Windows\“开始”菜单\程序\Pocket Word.lnk
0
Pocket Excel
\Windows\“开始”菜单\程序\Pocket Excel.lnk
0
Windows Media
\Windows\“开始”菜单\程序\Windows Media.lnk
0
Jawbreaker
\Windows\“开始”菜单\程序\游戏\Jawbreaker.lnk
0
纸牌
\Windows\“开始”菜单\程序\游戏\纸牌.lnk
0
凌云壮志
\Windows\“开始”菜单\程序\游戏\凌云壮志.lnk
0
MSN
\Windows\“开始”菜单\程序\MSN Messenger.lnk
0
电话
\Windows\“开始”菜单\电话.lnk
0
百宝箱
\Windows\“开始”菜单\程序\百宝箱.lnk
0
短信备份
\Windows\“开始”菜单\程序\短信备份.lnk
0
金山词霸
\Windows\“开始”菜单\程序\金山词霸.lnk
0
移动证券
\Windows\“开始”菜单\程序\移动证券.lnk
0
Utils
0
1
1
1
1
1
1
16
0
70
0
14
0
\Windows\Start Menu
0
Music
0
1
1
1
1
1
1
16
0
70
0
14
0
\Windows\Start Menu
0
eBooks
0
1
1
1
1
1
1
16
0
70
0
14
0
\Windows\Start Menu
0
Misc
0
1
1
1
1
1
1
16
0
70
0
14
0
\Windows\Start Menu
0
我运行那个RAPICSharp.exe,显示说mscorees.dll无法被执行,请重新安装.Net Framework。可我用别的.Net的程序很正常啊
--------------------------------------------------------------
我装了opennetcf,但是没有找到这个OpenNETCF.Notification.dll,难道是版本不一样嘛?
我的opennetcf中包括的dll有:opennetcf.dll, opennetcf.Data.dll, opennetcf.Drawing.dll,
opennetcf.net.dll, opennetcf.phone.dll, opennetcf.visualBisic.dll, opennetcf.web.service2.dll, opennetcf.windowsce.forms.dll. opennetcf.xml.dll.
还有其他的吗?
我的opennetcf frameword 是1.4版本。
不是daxia找到没有
谢谢鼓励,呵呵:) 最近因为在创业,实在是太忙,一直没完整的时间去写东西,不过我还是每天都“严重”关注博客堂的:B
NotificationAction这个枚举对象中有Vibrate,可以让你的PPC振动起来,这样就不会叫不醒了吧,呵呵:)
郁闷在于我的ppc从来叫不醒我,所以每天的唤醒服务还是由我的7650承担。