Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Windows Phone: Binding Contact Image to ListBox

DZone's Guide to

Windows Phone: Binding Contact Image to ListBox

· Mobile Zone
Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

In previous article Part 67 - Windows Phone - Get Contact Details I discussed how to get contact details. In this article I will explain how to get image from Contacts and bind it to ListBox.

Let's write code:

Step 1:  Add a ListBox and a Image below Listbox inside contentpanel of MainPage.xaml. ListBox will contain a image and textblock. Image inside listbox will hold the image of Contacts and textblock inside listbox will hold Contacts displayname. The image element outside ListBox will be used to show how to bind the Contacts image to individual image element.

<ListBox Grid.Row="1" Name="ContactResultsData" ItemsSource="{Binding}" Height="400" Margin="10,224,0,0" VerticalAlignment="Top">
   <ListBox.ItemTemplate>
      <DataTemplate>
         <StackPanel Orientation="Horizontal" >
            <Border BorderThickness="2" HorizontalAlignment="Left" VerticalAlignment="Center" BorderBrush="{StaticResource PhoneAccentBrush}" >
               <Image Source="{Binding Converter={StaticResource ContactPictureConverter}}" Width="48" Height="48" Stretch="Fill" />
            </Border>
            <TextBlock Name="ContactResults" Text="{Binding Path=DisplayName, Mode=OneWay}" FontSize="{StaticResource PhoneFontSizeExtraLarge}" Margin="18,8,0,0" />
         </StackPanel>
      </DataTemplate>
   </ListBox.ItemTemplate>
</ListBox>


<Border BorderThickness="2" Height="60" Margin="10,-400,0,0" HorizontalAlignment="Left" BorderBrush="{StaticResource PhoneAccentBrush}" >
   <Image Name="Picture" Height="60" Width="60" HorizontalAlignment="Left" />
</Border>

Step 2: Add namespace of application in phone:PhoneApplicationPage element in MainPage.xaml.

xmlns:MyApp="clr-namespace:WP_Display_Contacts_Photo"

Step 3: Add below using directive in MainPage.xaml.cs

 

using Microsoft.Phone.UserData;
using System.Windows.Media.Imaging;

Step 4: Add below code inside constructor of MainPage.xaml.cs.


 

ContactResultsData.DataContext = null;
Contacts cons = new Contacts();
cons.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(Contacts_SearchCompleted);
cons.SearchAsync("", FilterKind.None, "Contacts Image Test");

Step 5: Contacts_SearchCompleted method has two sections first section is to bind the first contact image to image element in MainPage.xaml and the second section is to bind the contact result to Listbox.

void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
   try
   {
      //Bind image to image element
      Contact con = e.Results.First();
      BitmapImage img = new BitmapImage();
      img.SetSource(con.GetPicture());
      Picture.Source = img;

      //Bind the results to the list box that displays them in the UI
      ContactResultsData.DataContext = e.Results;
   }
   catch (System.Exception)
   {
   }
}

Step 6: Create below class below MainPage class of MainPage.xaml.cs. ContactPictureConverter class bind the data.

public class ContactPictureConverter : System.Windows.Data.IValueConverter
{
   public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
   {
      Contact c = value as Contact;
      if (c == null) return null;
      System.IO.Stream imageStream = c.GetPicture();
      if (null != imageStream)
      {
         return Microsoft.Phone.PictureDecoder.DecodeJpeg(imageStream);
      }
      return null;
   }

   public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
   {
      throw new NotImplementedException();
   }
}

Step 7: Now run the appliction and you will notice the Contact image is binded to ListBox.

This ends the article of binding Contacts image to Listbox.

Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.

Topics:

Published at DZone with permission of Sumit Dutta, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}