[WIndows Phone7]List pictures in ListBox

1. Add reference to Microsoft.Xna.Framework dll .

2. Replace Grid xaml code with the following code:

 <Grid x:Name="LayoutRoot">
    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,50,12,0"
          Background="Transparent">
        <ListBox x:Name="treeList"></ListBox>
    </Grid>
 </Grid>

3. C# code in MainPage.xaml.cs file:

 public partial class MainPage : PhoneApplicationPage
 {
    // Constructor
    public MainPage()
    {
        InitializeComponent();
        using (var library = new MediaLibrary())
        {
            ShowAlbum(library.RootPictureAlbum, "|");
        }
    }
 
    //Show album pictures as a tree
    void ShowAlbum(PictureAlbum theAlbum, string indention)
    {
        // Show Album Name
        treeList.Items.Add(string.Concat(indention, "Album: ", theAlbum.Name));
 
        // List Albums in this Album
        foreach (PictureAlbum subAlbum in theAlbum.Albums)
        {
            ShowAlbum(subAlbum, string.Concat(indention, "-"));
        }
 
        // List Pictures
        foreach (Picture thePicture in theAlbum.Pictures)
        {
            // Get the Picture Stream
            Stream imageStream = thePicture.GetThumbnail();
            // Wrap it with a BitmapImage object
            var bitmap = new BitmapImage();
            bitmap.SetSource(imageStream);
            // Create an Image element and set the bitmap
            var image = new Image();
            image.Width = 60;
            image.Height = 60;
            image.Source = bitmap;
 
            StackPanel outPanel = new StackPanel();
            StackPanel stackPanel = new StackPanel();
            stackPanel.Orientation = 
System.Windows.Controls.Orientation.Horizontal;             TextBlock textBlock = new TextBlock();             textBlock.Text = thePicture.Name;                             stackPanel.Children.Add(image);             stackPanel.Children.Add(textBlock);             outPanel.Orientation =
System.Windows.Controls.Orientation.Horizontal;             TextBlock indentionBlock = new TextBlock();             indentionBlock.Text = string.Concat(indention, "-");             outPanel.Children.Add(indentionBlock);             outPanel.Children.Add(stackPanel);             treeList.Items.Add(outPanel);         }     } }

4. Last but not the least:

blog comments powered by Disqus