Thao Tác Danh Bạ Trên Ứng Dụng Windows Phone 8 Với C#

Windows Phone 8 cung cấp API quản lý danh bạ thông qua không gian tên Windows.Phone.PersonalInformation. Hai thành phần chính bao gồm:

  • ContactStore: Quản lý kho lưu trữ danh bạ ứng dụng, đóng vai trò trung gian cho các thao tác dữ liệu
  • StoredContact: Đại diện cho từng liên hệ được tạo bởi ứng dụng, kế thừa từ IContactInformation

Để truy cập danh bạ hệ thống, cần kích hoạt quyền ID_CAP_CONTACTS trong tập tin WMAppManifest.xml.

Tìm kiếm liên hệ

private async void TimKiemLienHe()
{
    var danhBa = new Contacts();
    danhBa.SearchCompleted += XuLyKetQuaTimKiem;
    danhBa.SearchAsync("090", FilterKind.PhoneNumber, "Tìm kiếm số");
}

private void XuLyKetQuaTimKiem(object sender, ContactsSearchEventArgs e)
{
    danhSachLienHe.ItemsSource = e.Results;
}

Thêm mới liên hệ

Có hai phương pháp chính để tạo liên hệ:

public async Task ThemLienHe(string ho, string ten)
{
    var khoLuu = await ContactStore.CreateOrOpenAsync(
        ContactStoreSystemAccessMode.ReadWrite,
        ContactStoreApplicationAccessMode.LimitedWrite);
    
    var lienHe = new StoredContact(khoLuu)
    {
        Ho = ho,
        Ten = ten
    };

    var thuocTinh = await lienHe.GetPropertiesAsync();
    thuocTinh.Add(KnownContactProperties.Telephone, "0888123456");
    thuocTinh.Add(KnownContactProperties.Email, "user@example.com");

    await lienHe.SaveAsync();
}

Phương pháp thay thế sử dụng ContactInformation:

var thongTin = new ContactInformation();
var props = await thongTin.GetPropertiesAsync();
props.Add(KnownContactProperties.GivenName, "Nguyễn");
props.Add(KnownContactProperties.Telephone, "0912345678");

var khoLuu = await ContactStore.CreateOrOpenAsync();
var lienHeMoi = new StoredContact(khoLuu, thongTin);
await lienHeMoi.SaveAsync();

Cập nhật thông tin

private async Task CapNhatLienHe(string id, string emailMoi)
{
    var khoLuu = await ContactStore.CreateOrOpenAsync();
    var lienHe = await khoLuu.FindContactByIdAsync(id);
    
    if (lienHe != null)
    {
        var thuocTinh = await lienHe.GetPropertiesAsync();
        thuocTinh[KnownContactProperties.Email] = emailMoi;
        await lienHe.SaveAsync();
    }
}

Xóa liên hệ

public async Task XoaLienHe(string contactId)
{
    var khoLuu = await ContactStore.CreateOrOpenAsync();
    await khoLuu.DeleteContactAsync(contactId);
}

Truy vấn danh sách

private async void LayDanhSachLienHe()
{
    var khoLuu = await ContactStore.CreateOrOpenAsync();
    var ketQuaTruyVan = khoLuu.CreateContactQuery();
    
    var tongSo = await ketQuaTruyVan.GetContactCountAsync();
    var danhSach = await ketQuaTruyVan.GetContactsAsync();
    
    danhSachLienHe.ItemsSource = danhSach;
}

Để truy xuất thuộc tính cụ thể của liên hệ:

private async Task HienThiSoDienThoai(StoredContact lienHe)
{
    var thuocTinh = await lienHe.GetPropertiesAsync();
    if (thuocTinh.ContainsKey(KnownContactProperties.Telephone))
    {
        MessageBox.Show($"SĐT: {thuocTinh[KnownContactProperties.Telephone]}");
    }
}

Thẻ: windows-phone-8 c-sharp contact-api async-programming mobile-development

Đăng vào ngày 7 tháng 6 lúc 19:39