Windows Mobile 6.5 Mother of all tutorial : Part 5 การใช้ตารางนัดหมาย (Calendar) และสิ่งที่ต้องทำ (Task)

Posted 13/01/2010 16:34 by nantcom

mofat5

การเขียนโปรแกรมเพื่อแก้ไข และอ่าน Appointment และ Task บน Windows Mobile ด้วย C# หรือ VB.NET โดยการใช้ .NET Compact Framework ก็ต้องทำผ่านระบบ Pocket Outlook เช่นเดียวกันครับ ซึ่งการใช้ Pocket Outlook ก็สามารถทำได้โดยการ Add Reference Microsoft.WindowsMobile.PocketOutlook เข้ามา

(ตอน Add Reference ระวังด้วยนะครับ เพราะว่ามันมี 2 ตัว เลือกตัวที่อยู่ใน SDK ของ WM6 นะครับ)

image

ตารางนัดหมาย (Calendar)

imageคลาสที่เราจะทำงานด้วย สำหรับการแก้ไขและอ่านตารางนัดหมาย ก็คือคลาส Microsoft.WindowsMobile.PocketOutlook.Appointment ครับ ซึ่งมีโครงสร้างตามด้านซ้ายมือนี้ Microsoft เขาก็ได้ทำการเก็บค่าต่างๆ ของการนัดกมาย ทำเป็น Property ให้เราหมดแล้ว ใช้ง่ายมากครับ

สำหรับการสร้าง Contact ใหม่ มีโค๊ดดังนี้ครับ

สร้าง Appointment ใหม่
  1. OutlookSession session = new OutlookSession();
  2.  
  3. Appointment appointment = new Appointment();
  4. appointment.Subject = "Meeting";
  5. appointment.Start = DateTime.Now;
  6. appointment.End = new DateTime(2010, 1, 15);
  7.  
  8. session.Appointments.Items.Add(appointment);
  9. session.Dispose();

สังเกตว่าเราจะต้องสั่ง Dispose Session เพื่อ Clear หน่วยความจำที่ใช้ออกด้วยครับ

ส่วนถ้าต้องการอ่าน Appointment ในเครื่อง ก็ใช้โค๊ดดังนี้ครับ

อ่าน Appointment ในเครื่อง
  1. OutlookSession session = new OutlookSession();
  2. for (int i = 0; i < session.Appointments.Items.Count; i++)
  3. {
  4.     session.Appointments.ItemsIdea.Body = "string";
  5.  
  6.     session.Appointments.ItemsIdea.Update();
  7. }
  8. session.Dispose();


สังเกตว่า หลังการแก้ไข จะต้องสั่ง Update() เพื่อบันทึกค่าลงในระบบด้วยครับ

สิ่งที่ต้องทำ (Task)

image

ผมว่า ไม่ต้องบอก ก็คงเดาได้แล้วใช่ไหมครับ การที่จะใช้งาน Task ก็ต้องทำงานกับคลาส Microsoft.WindowsMobile.PocketOutlook.Task ครับ ซึ่งโครงสร้าง ก็ตามด้านซ้ายครับ และการทำงาน ก็เหมือนกันราวกับแกะเลยทีเดียว

การสร้าง Task ใหม่ ทำได้ดังนี้ครับ

สร้าง Task ใหม่
  1. OutlookSession session = new OutlookSession();
  2.  
  3. Task task = new Task();
  4. task.Subject = "Subject";
  5. task.Body = "Text";
  6. task.DueDate = DateTime.Now;
  7.  
  8. session.Tasks.Items.Add(task);
  9. session.Dispose();

และการอ่าน Task ก็ทำได้ดังนี้ครับ

อ่าน Task ในเครื่อง
  1. OutlookSession session = new OutlookSession();
  2. for (int i = 0; i < session.Tasks.Items.Count; i++)
  3. {
  4.     session.Tasks.ItemsIdea.Body = "string";
  5.     session.Tasks.ItemsIdea.Update();
  6. }
  7. session.Dispose();

ซึ่งสาเหตุที่ทุกอย่าง ทำงานคล้ายกัน ก็เพราะว่า ทุกคลาส มัน Inherit มาจาก PIMItem นั่นเองครับ

image

Published Jan 13 2010, 04:34 PM by nantcom
Filed under:

ร่วมให้กำลังใจนักเขียน

อ่านแล้วชอบใจ อยากให้กำลังใจกับผู้แต่งบทความนี้ ขอเชิญร่วมให้กำลังใจผ่าน Paysbuy/Paypal นะครับ ปลอดภัยเพราะทำงานผ่าน SSL และไม่มีค่าใช้จ่ายเพิ่มเติมครับ เว็บเราให้นักเขียน 100% ครับ

Comment ระบบเก่า

 

welcome said:

&#160; &#160;&#160; Review : Toshiba TG01 สวัสดีปีใหม่ครับ!!!&#160; ปีใหม่นี้ อยากได้หูฟัง Bluetooth

January 14, 2010 12:13 AM
 

giffy2499 said:

wow ๆๆ น่าสนใจคับ

January 14, 2010 10:26 PM
(required)  
(optional)
(required)  
Add

DisQUS Comment (ยังเอ๋อๆ อยู่)

blog comments powered by Disqus