快速入門:選取使用者連絡人 (HTML)
[ 本文的目標對象是撰寫 Windows 執行階段 App 的 Windows 8.x 和 Windows Phone 8.x 開發人員。如果您正在開發適用於 Windows 10 的 App,請參閱 最新文件 ]
在整個 Windows.ApplicationModel.Contacts 命名空間中,有好幾種方法可以用來選取連絡人。我們在這裡會為您示範如何選取單一連絡人或多位連絡人,也會示範如何設定連絡選擇器只抓取您應用程式需要的連絡人資訊。
先決條件
- 我們建議您熟悉 Microsoft Visual Studio 以及相關範本。
- 我們建議您熟悉 JavaScript 開發。
設定連絡人選擇器
建立一個 Windows.ApplicationModel.Contacts.ContactPicker 實例,並將它指派給一個變數。
var picker = Windows.ApplicationModel.Contacts.ContactPicker();
提供認可按鈕的名稱
認可按鈕是使用者確認所選連絡人時所按的按鈕。以 Windows.ApplicationModel.Contacts.CommitButtonText 設定這個按鈕的名稱。
picker.commitButtonText = "Select";
設定選取模式 (選擇性)
根據預設,連絡人選擇器會抓取使用者選取之連絡人的所有可用資料。selectionMode 屬性讓您設定連絡人選擇器只抓取您應用程式需要的資料欄位。如果您只需要可用連絡人資料的子集,這是使用連絡人選擇器較有效率的方式。
首先,將 selectionMode 屬性設定成 fields:
picker.selectionMode = Windows.ApplicationModel.Contacts.ContactSelectionMode.fields;
然後,使用 desiredFieldsWithContactFieldType 屬性指定您想要連絡人選擇器抓取的欄位。這個範例會設定連絡人選擇器抓取電子郵件地址:
picker.desiredFieldsWithContactFieldType.append(Windows.ApplicationModel.Contacts.ContactFieldType.email);
啟動選擇器
現在您可以啟動選擇器。如果您只想讓使用者選取一位連絡人,請使用 pickContactAsync。若要處理使用者選取的連絡人,請使用 done 來呼叫函式,以處理選擇器傳回的連絡人。
picker.pickContactAsync().done(function (contact) {
if (contact !== null) {
// Create UI to display the contact information for the selected contact
var contactElement = document.createElement("div");
contactElement.className = "contact";
// Display the name
contactElement.appendChild(createTextElement("h3", contact.displayName));
如果您想讓使用者選取一或多位連絡人,請使用 pickContactsAsync。 若要處理使用者選取的連絡人,請使用 done 來呼叫函式,以處理選擇器傳回的連絡人。
picker.pickContactsAsync().done(function (contacts) {
if (contacts.length > 0) {
// Display the selected contact names
var output = "Selected contacts:\n";
contacts.forEach(function (contact) {
output += contact.displayName + "\n";
});
完整範例 (單一連絡人)
這個範例使用連絡人選擇器來抓取一位連絡人的姓名、電子郵件地址、電話號碼以及住址。
(function () {
"use strict";
var page = WinJS.UI.Pages.define("/html/scenarioSingle.html", {
ready: function (element, options) {
// Use element.querySelector() instead of document.getElementById() to ensure that the correct default.html page is targeted.
element.querySelector("#open").addEventListener("click", pickContact, false);
}
});
function pickContact() {
// Clean scenario output
WinJS.log && WinJS.log("", "sample", "status");
// Create the picker
var picker = new Windows.ApplicationModel.Contacts.ContactPicker();
picker.commitButtonText = "Select";
// Open the picker for the user to select a contact
picker.pickContactAsync().done(function (contact) {
if (contact !== null) {
// Create UI to display the contact information for the selected contact
var contactElement = document.createElement("div");
contactElement.className = "contact";
// Display the name
contactElement.appendChild(createTextElement("h3", contact.displayName));
// Add the different types of contact data
if (contact.emails.length > 0) {
contactElement.appendChild(createTextElement("h4", "Emails:"));
contact.emails.forEach(function (email) {
switch (email.kind) {
case Windows.ApplicationModel.Contacts.ContactEmailKind.personal:
contactElement.appendChild(createTextElement("div", email.address + " (personal)"));
break;
case Windows.ApplicationModel.Contacts.ContactEmailKind.work:
contactElement.appendChild(createTextElement("div", email.address + " (work)"));
break;
case Windows.ApplicationModel.Contacts.ContactEmailKind.other:
contactElement.appendChild(createTextElement("div", email.address + " (other)"));
break;
}
});
}
if (contact.phones.length > 0) {
contactElement.appendChild(createTextElement("h4", "Phone Numbers:"));
contact.phones.forEach(function (phone) {
switch (phone.kind) {
case Windows.ApplicationModel.Contacts.ContactPhoneKind.home:
contactElement.appendChild(createTextElement("div", phone.number + " (home)" ));
break;
case Windows.ApplicationModel.Contacts.ContactPhoneKind.work:
contactElement.appendChild(createTextElement("div", phone.number + " (work)" ));
break;
case Windows.ApplicationModel.Contacts.ContactPhoneKind.mobile:
contactElement.appendChild(createTextElement("div", phone.number + " (mobile)" ));
break;
case Windows.ApplicationModel.Contacts.ContactPhoneKind.other:
contactElement.appendChild(createTextElement("div", phone.number + " (other)" ));
break;
}
});
}
if (contact.addresses.length > 0) {
contactElement.appendChild(createTextElement("h4", "Addresses:"));
contact.addresses.forEach(function (address) {
switch (address.kind) {
case Windows.ApplicationModel.Contacts.ContactAddressKind.home:
contactElement.appendChild(createTextElement("div", getUnstructuredAddress(address) + " (home)" ));
break;
case Windows.ApplicationModel.Contacts.ContactAddressKind.work:
contactElement.appendChild(createTextElement("div", getUnstructuredAddress(address) + " (work)"));
break;
case Windows.ApplicationModel.Contacts.ContactAddressKind.other:
contactElement.appendChild(createTextElement("div", getUnstructuredAddress(address) + " (other)"));
break;
}
});
}
// Add the contact element to the page
document.getElementById("output").appendChild(contactElement);
} else {
// The picker was dismissed without selecting a contact
WinJS.log && WinJS.log("No contact was selected", "sample", "status");
}
});
}
function createTextElement(tag, text) {
var element = document.createElement(tag);
element.className = "singleLineText";
element.innerText = text;
return element;
}
function getUnstructuredAddress(contactAddress) {
var unstructuredAddress = contactAddress.streetAddress + ", " + contactAddress.locality + ", " + contactAddress.region + ", " + contactAddress.postalCode;
return unstructuredAddress;
}
})();
完整範例 (多位連絡人)
這個範例使用連絡人選擇器來抓取多位連絡人,然後顯示連絡人姓名。
(function () {
"use strict";
var page = WinJS.UI.Pages.define("/html/scenarioMultiple.html", {
ready: function (element, options) {
// Use element.querySelector() instead of document.getElementById() to ensure that the correct default.html page is targeted.
element.querySelector("#open").addEventListener("click", pickContacts, false);
}
});
function pickContacts() {
// Clean scenario output
WinJS.log && WinJS.log("", "sample", "status");
// Create the picker
var picker = new Windows.ApplicationModel.Contacts.ContactPicker();
picker.commitButtonText = "Select";
// Open the picker for the user to select contacts
picker.pickContactsAsync().done(function (contacts) {
if (contacts.length > 0) {
// Display the selected contact names
var output = "Selected contacts:\n";
contacts.forEach(function (contact) {
output += contact.displayName + "\n";
});
WinJS.log && WinJS.log(output, "sample", "status");
} else {
// The picker was dismissed without selecting any contacts
WinJS.log && WinJS.log("No contacts were selected", "sample", "status");
}
});
}
})();
摘要與後續步驟
現在您已基本了解如何使用連絡人選擇器來抓取連絡人資訊。請從程式碼庫下載連絡人選擇器應用程式範例,查看如何使用連絡人和連絡人選擇器的更多範例。