Windows Phone 7 电话本相关的操作

抽一点时间学习学习 WP7,由于自己的 C# 基础很差,加上工作的原因,所以学习的进度很慢。

慢就慢吧,慢慢来。呵呵....

 

这两个在 WP7 对电话本的编程进行学习,主要是读取电话本和操作电话。但发现在 WP7 下对电话本的操作太受限制了,不太适应。

 

以下代码,主要是读取电话本,和通过 SaveContactTask 新增电话本。网上说没有办法通过代码实现编辑、删除等操作,唉...

 以下代码中,有两处编译未通过。虽然不影响功能,但还是想知道为什么不行,以后有空继续吧。

  1 #define _SIMPLE_OUTPUT_PB_  
  2   
  3 using System;  
  4 using System.Collections.Generic;  
  5 using System.Linq;  
  6 using System.Net;  
  7 using System.Windows;  
  8 using System.Windows.Controls;  
  9 using System.Windows.Documents;  
 10 using System.Windows.Input;  
 11 using System.Windows.Media;  
 12 using System.Windows.Media.Animation;  
 13 using System.Windows.Shapes;  
 14 using Microsoft.Phone.Controls;  
 15   
 16 using Microsoft.Phone.UserData;  
 17 // using System.d  
 18 using Microsoft.Phone.Tasks;  
 19   
 20 namespace PhoneBookOp  
 21 {  
 22     public partial class MainPage : PhoneApplicationPage  
 23     {  
 24         // 构造函数  
 25         public MainPage()  
 26         {  
 27             InitializeComponent();  
 28         }  
 29  
 30 #if (_SIMPLE_OUTPUT_PB_)  
 31       // 注意啊,下面两个函数是不相同的。当时写的时候没有注意,大小区别。  
 32         void ContactsSearchCompleted(object sender, ContactsSearchEventArgs e)  
 33         {  
 34             foreach (var varResult in e.Results)  
 35             {  
 36                 System.Diagnostics.Debug.WriteLine("Name: " + varResult.DisplayName + "Phone number: " + varResult.PhoneNumbers.FirstOrDefault());  
 37                 listBox1.Items.Add(varResult.DisplayName + " " + varResult.PhoneNumbers.FirstOrDefault());  
 38             }  
 39         }  
 40         private void contactsSearchCompleted(object sender, ContactsSearchEventArgs e)  
 41         {  
 42             foreach (Contact contactResult in e.Results)  
 43             {  
 44                 string name = contactResult.DisplayName;  
 45                 ContactPhoneNumber phoneNumber = contactResult.PhoneNumbers.FirstOrDefault();  
 46                 string strPhone = string.Empty;  
 47                 if (null != phoneNumber)  
 48                 {  
 49                     strPhone = phoneNumber.PhoneNumber;  
 50                 }  
 51                 /*ContactAddress address = contactResult.Addresses.FirstOrDefault(); 
 52                 string strAddr = string.Empty; 
 53                 if (null != address) 
 54                 { 
 55                     strAddr = address.PhysicalAddress.AddressLine1;     // 此行未编译通过 System.Device 无法被 using 
 56                 }*/  
 57                 ContactEmailAddress emailAddress = contactResult.EmailAddresses.FirstOrDefault();  
 58                 string strEmail = string.Empty;  
 59                 if (null != emailAddress)  
 60                 {  
 61                     strEmail = emailAddress.EmailAddress;  
 62                 }  
 63                 // System.Diagnostics.Debug.WriteLine("Number:" + strPhone + ";Address:" + strAddr + ";EMail:" + strEmail);  
 64                 System.Diagnostics.Debug.WriteLine("Number:" + strPhone + ";EMail:" + strEmail);  
 65             }  
 66         }  
 67         void saveContactCompleted(object sender, SaveContactResult e)  
 68         {  
 69             switch (e.TaskResult)  
 70             {  
 71                 case TaskResult.OK:  
 72                     break;  
 73                 case TaskResult.Cancel:  
 74                     break;  
 75                 case TaskResult.None:  
 76                     break;  
 77                 default:  
 78                     break;  
 79             }  
 80         }  
 81   
 82         private void SearchPhoneBookClick(object sender, RoutedEventArgs e)  
 83         {  
 84             Contacts GetContacts = new Contacts();  
 85             GetContacts.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(ContactsSearchCompleted);  
 86             GetContacts.SearchAsync(string.Empty, FilterKind.None, null);  
 87         }  
 88   
 89         private void SearchPhoneBookClick2(object sender, RoutedEventArgs e)  
 90         {  
 91             Contacts contacts = new Contacts();  
 92             contacts.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(contactsSearchCompleted);  
 93             contacts.SearchAsync(string.Empty, FilterKind.None, null);  
 94         }  
 95   
 96         private void AddPhoneBook(object sender, RoutedEventArgs e)  
 97         {  
 98             SaveContactTask saveContact = new SaveContactTask();  
 99             saveContact.Completed +=new EventHandler<SaveContactResult>(saveContactCompleted);  
100   
101             saveContact.FirstName = "Leo.Zheng";  
102             saveContact.MobilePhone = "18665881061";  
103             saveContact.HomeAddressStreet = "广东省深圳市";  
104             saveContact.PersonalEmail = "doves@126.com";  
105             saveContact.Show();  
106         }  
107 #else  
108         void objContacts_SearchCompleted(object sender, ContactsSearchEventArgs e)  
109         {  
110             var ContactsData = from m in e.Results  
111                                select new MyContacts  
112                                    {  
113                                        DisplayName = m.DisplayName,  
114                                        PhoneNumber = m.PhoneNumbers.FirstOrDefault()  
115                                    };  
116             var MyContactsList = from contact in ContactsData   
117                                  group contact by contact.DisplayName into c   
118                                  orderby c.Key select new Group<MyContacts>(c.Key,c);       // 此句编译不通过  
119             listBox1.ItemsSource = ContactsData;  
120         }  
121   
122         public class MyContacts  
123         {  
124             public string DisplayName { get; set; }  
125             public ContactPhoneNumber PhoneNumber { get; set; }  
126         }  
127 #endif  
128     }  
129 }  

 

posted @ 2016-02-22 10:05  91program  阅读(173)  评论(0编辑  收藏  举报