I have this scenario, in my windows phone App.
I have a function which needs to return the DisplayName of a contact, when provided with the PhoneNumber.
public string GetDisplayName(string number)
{
Contacts contactsBook = new Contacts();
contactsBook.SearchCompleted += contactsBook_SearchCompleted;
contactsBook.SearchAsync(number, FilterKind.PhoneNumber, null);
//return .....; How will I be able to return the display name here?
}
As SearchAsync return type is void, I cannot use the await keyword here. So how would I be able to design the function here?
Is it recommended pattern to use some thread signaling method here? (I would wait on an event after SearchAsync method and I will fire the event in SearchCompleted event)
The reason, I would want such a function is because, I get a set of numbers from my server and I would display the corresponding name in the UI.
<phone:LongListSelector ItemsSource="{Binding PhoneNumberCollection}">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding PhoneNumber, Converter={StaticResource PhoneNumberToNameConverter}}" />
</Grid>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
and I would have the IValueConverter converter as below
public class PhoneNumberToNameConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return ContactsManager.Instance.GetDisplayName((string)value);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}