0

在 windows phone 8 中从手机中读取联系人只有以下代码:

Dim WithEvents objContacts As New Microsoft.Phone.UserData.Contacts

Private Sub ReadContacts
objContacts.SearchAsync("", Microsoft.Phone.UserData.FilterKind.None, Nothing)
'Result will be read from the event
End Sub

'Event 

Private Sub A_SearchCompleted(sender As Object, e As Microsoft.Phone.UserData.ContactsSearchEventArgs) Handles A.SearchCompleted
Dim B = e.Results.ToList
End Sub

我的问题是:如何将该功能转换为类中的可等待函数?

例子:

Public Class Contacto

Public Async Function GetContacts() As System.Threading.Tasks.Task(Of List(Of Microsoft.Phone.UserData.Contact))

'Do some work: here's my question

End Function

End Class 


'So I can call my function


Dim o as new Contacto

dim Contacts = Await o.GetContacts()

非常感谢,感谢您的回复。

4

2 回答 2

0

如果要在函数中调用 await ,则需要添加 async 关键字

public async void functionname () //c# syntax
于 2012-11-23T04:55:55.587 回答
0

我通过使用 System.Threading.Tasks.TaskCompletionSource 类找到了我的问题的解决方案

Public Class CustomContacts
        Dim WithEvents objContacts As New Microsoft.Phone.UserData.Contacts
        Dim tcs As New System.Threading.Tasks.TaskCompletionSource(Of List(Of Microsoft.Phone.UserData.Contact))

        Public Async Function GetContacts() As System.Threading.Tasks.Task(Of List(Of Microsoft.Phone.UserData.Contact))
            objContacts.SearchAsync("", Microsoft.Phone.UserData.FilterKind.None, Nothing)
            Dim ListContacts = Await tcs.Task

           Return ListContacts
        End Function

        Private Sub objContacts_SearchCompleted(sender As Object, e As Microsoft.Phone.UserData.ContactsSearchEventArgs) Handles objContacts.SearchCompleted
            tcs.SetResult(e.Results.ToList)
        End Sub
    End Class


'So now I can call the function as follow:

dim objContacts as new CustomContacts
Dim myContacts = Await objContacts.GetContacts 'returns List(Of Microsoft.Phone.UserData.Contact)
于 2012-11-23T22:33:46.090 回答