根据 Likness(第 164 页,“使用 C# 和 XAML 构建 Windows 8 应用程序”),“执行异步任务时,您必须请求延期。”
所以,如果我没有把他断章取意,这段代码:
private async Task<System.Collections.Generic.KeyValuePair<string, string>> SelectAContactForASlot()
{
KeyValuePair<string, string> kvp;
var contactPicker = new Windows.ApplicationModel.Contacts.ContactPicker();
contactPicker.CommitButtonText = "Select";
var contact = await contactPicker.PickSingleContactAsync();
if (contact != null)
{
kvp = new KeyValuePair<string, string>(contact.Name, contact.Emails[0].ToString());
return kvp;
}
return kvp = new KeyValuePair<string, string>("No Name found", "No email found");
}
...应该是这样的:
private async Task<System.Collections.Generic.KeyValuePair<string, string>> SelectAContactForASlot()
{
var deferral = e.SuspendingOperation.GetDeferral();
KeyValuePair<string, string> kvp;
var contactPicker = new Windows.ApplicationModel.Contacts.ContactPicker();
contactPicker.CommitButtonText = "Select";
var contact = await contactPicker.PickSingleContactAsync();
if (contact != null)
{
kvp = new KeyValuePair<string, string>(contact.Name, contact.Emails[0].ToString());
return kvp;
}
return kvp = new KeyValuePair<string, string>("No Name found", "No email found");
deferral.Complete();
}
正确的?