抽一點時間學習學習 WP7,由於自己的 C# 基礎很差,加上工作的原因,所以學習的進度很慢。
慢就慢吧,慢慢來。呵呵....
這兩個在 WP7 對電話本的編程進行學習,主要是讀取電話本和操作電話。但發現在 WP7 下對電話本的操作太受限制了,不太適應。
以下代碼,主要是讀取電話本,和通過 SaveContactTask 新增電話本。網上說沒有辦法通過代碼實現編輯、刪除等操作,唉...
以下代碼中,有兩處編譯未通過。雖然不影響功能,但還是想知道為什麼不行,以後有空繼續吧。
#define _SIMPLE_OUTPUT_PB_using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using Microsoft.Phone.Controls;using Microsoft.Phone.UserData;// using System.dusing Microsoft.Phone.Tasks;namespace PhoneBookOp{ public partial class MainPage : PhoneApplicationPage { // 建構函式 public MainPage() { InitializeComponent(); }#if (_SIMPLE_OUTPUT_PB_)
// 注意啊,下面兩個函數是不相同的。當時寫的時候沒有注意,大小區別。 void ContactsSearchCompleted(object sender, ContactsSearchEventArgs e) { foreach (var varResult in e.Results) { System.Diagnostics.Debug.WriteLine("Name: " + varResult.DisplayName + "Phone number: " + varResult.PhoneNumbers.FirstOrDefault()); listBox1.Items.Add(varResult.DisplayName + " " + varResult.PhoneNumbers.FirstOrDefault()); } } private void contactsSearchCompleted(object sender, ContactsSearchEventArgs e) { foreach (Contact contactResult in e.Results) { string name = contactResult.DisplayName; ContactPhoneNumber phoneNumber = contactResult.PhoneNumbers.FirstOrDefault(); string strPhone = string.Empty; if (null != phoneNumber) { strPhone = phoneNumber.PhoneNumber; } /*ContactAddress address = contactResult.Addresses.FirstOrDefault(); string strAddr = string.Empty; if (null != address) { strAddr = address.PhysicalAddress.AddressLine1; // 此行未編譯通過 System.Device 無法被 using }*/ ContactEmailAddress emailAddress = contactResult.EmailAddresses.FirstOrDefault(); string strEmail = string.Empty; if (null != emailAddress) { strEmail = emailAddress.EmailAddress; } // System.Diagnostics.Debug.WriteLine("Number:" + strPhone + ";Address:" + strAddr + ";EMail:" + strEmail); System.Diagnostics.Debug.WriteLine("Number:" + strPhone + ";EMail:" + strEmail); } } void saveContactCompleted(object sender, SaveContactResult e) { switch (e.TaskResult) { case TaskResult.OK: break; case TaskResult.Cancel: break; case TaskResult.None: break; default: break; } } private void SearchPhoneBookClick(object sender, RoutedEventArgs e) { Contacts GetContacts = new Contacts(); GetContacts.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(ContactsSearchCompleted); GetContacts.SearchAsync(string.Empty, FilterKind.None, null); } private void SearchPhoneBookClick2(object sender, RoutedEventArgs e) { Contacts contacts = new Contacts(); contacts.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(contactsSearchCompleted); contacts.SearchAsync(string.Empty, FilterKind.None, null); } private void AddPhoneBook(object sender, RoutedEventArgs e) { SaveContactTask saveContact = new SaveContactTask(); saveContact.Completed +=new EventHandler<SaveContactResult>(saveContactCompleted); saveContact.FirstName = "Leo.Zheng"; saveContact.MobilePhone = "18665881061"; saveContact.HomeAddressStreet = "廣東省深圳市"; saveContact.PersonalEmail = "doves@126.com"; saveContact.Show(); }#else void objContacts_SearchCompleted(object sender, ContactsSearchEventArgs e) { var ContactsData = from m in e.Results select new MyContacts { DisplayName = m.DisplayName, PhoneNumber = m.PhoneNumbers.FirstOrDefault() }; var MyContactsList = from contact in ContactsData group contact by contact.DisplayName into c orderby c.Key select new Group<MyContacts>(c.Key,c); // 此句編譯不通過 listBox1.ItemsSource = ContactsData; } public class MyContacts { public string DisplayName { get; set; } public ContactPhoneNumber PhoneNumber { get; set; } }#endif }}