Windows Phone 8 将允许开发人员创建自己的Custom Contact Store。应用程序通过应用程序添加新联系人后,先联系人出现在Windows Phone hub中,并和用户的系统联系人并存。开发人员可以为创建联系人的的标准属性,如电话号码、名称等,也可以存储自定义属性。开发人员还可以利用联系人存储api来同步他们的用户的联系人列表到云端。
创建联系人:
private async void Button_Click_4(object sender, RoutedEventArgs e)
{
//var store = await ContactStore.CreateOrOpenAsync(ContactStoreSystemAccessMode.ReadOnly,
// ContactStoreApplicationAccessMode.LimitedReadOnly);
//链接并打开联系人, 需要添加<Capability Name="ID_CAP_CONTACTS" /> 能力
var store = await ContactStore.CreateOrOpenAsync();
Debug.WriteLine("RevisionNumber:" + store.RevisionNumber);
StoredContact sc = new StoredContact(store);
sc.DisplayName = "Mark";
sc.HonorificPrefix = "et";
await sc.SaveAsync();
}
读取联系人
private async void Button_Click_2(object sender, RoutedEventArgs e)
{
//var store = await ContactStore.CreateOrOpenAsync(ContactStoreSystemAccessMode.ReadOnly,
// ContactStoreApplicationAccessMode.LimitedReadOnly);
//链接并打开联系人, 需要添加<Capability Name="ID_CAP_CONTACTS" /> 能力
var store = await ContactStore.CreateOrOpenAsync();
Debug.WriteLine("RevisionNumber:" + store.RevisionNumber);
//创建联系人查询
ContactQueryResult result = store.CreateContactQuery();
var count = await result.GetContactCountAsync();
Debug.WriteLine("GetContactCountAsync:" + count);
ContactQueryOptions option = result.GetCurrentQueryOptions();
foreach (string filed in option.DesiredFields)
{
Debug.WriteLine("filed:" + filed);
}
//获取联系人列表
var contacts = await result.GetContactsAsync();
foreach (StoredContact contact in contacts)
{
Debug.WriteLine("DisplayName:" + contact.DisplayName);
//将联系人数据转换成VCard文件
var vCard = await contact.ToVcardAsync();
IInputStream inputStream = vCard.GetInputStreamAt(0);
ulong length = vCard.Size;
try
{
//将vCard文件流使用字符串输出
var readBuf = new Windows.Storage.Streams.Buffer((uint)length);
var vCardOp = vCard.GetInputStreamAt(0).ReadAsync(readBuf, (uint)length, InputStreamOptions.Partial);
vCardOp.Completed = (IAsyncOperationWithProgress<IBuffer, uint> asyncAction, AsyncStatus asyncStatus) =>
{
switch (asyncStatus)
{
case AsyncStatus.Completed:
Debug.WriteLine("vCardString:" + MainPage.BufferToString(readBuf));
break;
case AsyncStatus.Error:
break;
case AsyncStatus.Canceled:
// Read is not cancelled in this sample.
break;
}
};
}
catch (Exception exp)
{
Debug.WriteLine(exp.ToString());
}
}
}