在Twitter上,Justin Angel暗示能夠解決已知的Windows Phone SDK限制,您無法啓動將顯示給定聯繫人(vote to fix this issue here)的詳細信息的ShowContactTask。 I threw together a test on my own嘗試使此功能正常工作,但它僅顯示帶有用戶名的空白頁。啓動WP8中特定聯繫人的詳細信息頁面
有人有機會舉例說明如何做這項工作?
在此先感謝!
在Twitter上,Justin Angel暗示能夠解決已知的Windows Phone SDK限制,您無法啓動將顯示給定聯繫人(vote to fix this issue here)的詳細信息的ShowContactTask。 I threw together a test on my own嘗試使此功能正常工作,但它僅顯示帶有用戶名的空白頁。啓動WP8中特定聯繫人的詳細信息頁面
有人有機會舉例說明如何做這項工作?
在此先感謝!
正如我在twitter上所說的,WP8不允許從自定義啓動器任務或通過app2app自定義協議啓動聯繫人詳細信息。
人員中心支持在與聯繫人詳細信息屏幕非常相似的屏幕中導入vCard。您可以使用此功能主要導入vCard或可選地顯示聯繫人詳細信息(即使它將顯示爲新聯繫人)。這裏是爲人民樞紐名片app2app文件關聯:
<Extensions>
<FileTypeAssociation Name="VCard" Category="phone.fileTypeAssociation" TaskID="ViewVCardContact" NavUriFragment="VCardMode=2&vCardFileToken=%s">
<Logos>
<Logo Size="small" IsResource="true">res://ContactsRes{ScreenResolution}!VCard.FileTypeIcon.Small.png</Logo>
<Logo Size="medium" IsResource="true">res://ContactsRes{ScreenResolution}!VCard.FileTypeIcon.Medium.png</Logo>
<Logo Size="large" IsResource="true">res://ContactsRes{ScreenResolution}!VCard.FileTypeIcon.Large.png</Logo>
</Logos>
<SupportedFileTypes>
<FileType ContentType="text/vcard">.vcf</FileType>
<FileType ContentType="text/x-vcard">.vcf</FileType>
</SupportedFileTypes>
</FileTypeAssociation>
</Extensions>
爲了使用,我們可以寫上去,創建一個電子名片VCF文件,將其保存到IsoStore並啓動在人們的名片一些代碼這個app2app自定義協議樞紐。
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
var contacts = new Contacts();
contacts.SearchCompleted += contacts_SearchCompleted;
contacts.SearchAsync(string.Empty, FilterKind.None, null);
}
private async void contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
// get a Contact and build a vCard for it
Contact contact = e.Results.First();
string vcard = GetContactVCard(contact);
Debug.WriteLine(vcard);
// write the vCard to IsoStore as a VCF file
using (var s = IsolatedStorageFile.GetUserStoreForApplication().CreateFile("myContact.vcf"))
using (var sw = new StreamWriter(s))
{
sw.Write(vcard);
}
// launch the VCF file
var vcardStorageFile = await ApplicationData.Current.LocalFolder.GetFileAsync("myContact.vcf");
Launcher.LaunchFileAsync(vcardStorageFile);
}
接下來我們需要實現構建vCard的函數。我們將使用vCard版本3.0,因爲我們需要Base64嵌入式圖像(稍後會詳細介紹)。我從維基百科複製了vCard 3.0格式,所以這個例子沒有任何Microsoft特定的擴展。我只是將vCard複製到代碼中,並從Contact類中注入了一些數據。
private string GetContactVCard(Contact contact)
{
return string.Format(@"BEGIN:VCARD
VERSION:3.0
N:{0};{1}
FN:{2}
ORG:{3}
TITLE:{4}
PHOTO;TYPE=PNG;ENCODING=B:{9}
TEL;TYPE=WORK,VOICE:{5}
TEL;TYPE=HOME,VOICE:{6}
ADR;TYPE=WORK:;;{10}
ADR;TYPE=HOME:;;{11}
EMAIL;TYPE=PREF,INTERNET:{7}
REV:{8}
END:VCARD",
contact.CompleteName.LastName,
contact.CompleteName.FirstName,
contact.DisplayName,
contact.Companies.Any() ? contact.Companies.First().CompanyName : string.Empty,
contact.CompleteName.Title,
contact.PhoneNumbers.Any(p => p.Kind == PhoneNumberKind.Work) ? contact.PhoneNumbers.First(p => p.Kind == PhoneNumberKind.Work).PhoneNumber : string.Empty,
contact.PhoneNumbers.Any(p => p.Kind != PhoneNumberKind.Work) ? contact.PhoneNumbers.First(p => p.Kind != PhoneNumberKind.Work).PhoneNumber : string.Empty,
contact.EmailAddresses.Any() ? contact.EmailAddresses.First().EmailAddress : string.Empty,
DateTime.Now.ToFileTimeUtc(),
GetPhotoBase64(contact),
contact.Addresses.Any(a => a.Kind == AddressKind.Home) ? BuildAddress(contact.Addresses.First(a => a.Kind == AddressKind.Home).PhysicalAddress) : string.Empty,
contact.Addresses.Any(a => a.Kind == AddressKind.Work) ? BuildAddress(contact.Addresses.First(a => a.Kind == AddressKind.Work).PhysicalAddress) : string.Empty
);
}
你可以看到我沒有花太多時間來完善這個邏輯。如果您添加了一些邊緣條件處理並可能移動到StringBuilder,可能會更好。下一步,我們將建立較爲複雜地址字符串:
private string BuildAddress(CivicAddress physicalAddress)
{
StringBuilder sb = new StringBuilder();
string[] address = new string[]
{
physicalAddress.AddressLine1,
physicalAddress.AddressLine2,
physicalAddress.City + " " + physicalAddress.StateProvince,
physicalAddress.CountryRegion,
physicalAddress.PostalCode
};
foreach (var line in address)
if (!string.IsNullOrEmpty(line))
sb.Append(line + ";");
return sb.ToString();
}
最後,我們將需要將圖像作爲我們的電子名片的一部分。我們將聯繫人照片嵌入到base64中,因爲WP8沒有可用於該聯繫人照片的網址。
private string GetPhotoBase64(Contact contact)
{
using (var picture = contact.GetPicture())
{
if (picture == null)
{
return null;
}
else
{
using (var sr = new StreamReader(picture))
{
byte[] data = new byte[picture.Length];
picture.Read(data, 0, data.Length);
return Convert.ToBase64String(data);
}
}
}
}
當我們運行該代碼片段,我們可以看到它承載了大部分的聯繫人的屬性的細節到導出/導入電子名片:
一個問題要問您的代碼段,我的編譯器在Extensions定義中無法識別Category =「phone.fileTypeAssociation」,你知道爲什麼嗎? – sunjinbo
上面列出的 XML是人員集線器中的一個,而不是您的應用。這是爲了向您展示如何通過文件關聯與人員中心進行交互。 –
JustinAngel