locked
Album Art Work Through Data Binding

    Question

  • Hello, I have done this

    Me.DataContext = file

    to set the files up for data bimding, but how do I get the album Art work as my image? I need to know what path to type in to the data context bit when you create a new data binding? any help will be great

    thank you in advance

    jordan

    Thursday, February 27, 2014 10:04 PM

Answers

  • One options would be to use a converter. i.e.

    (For c#)

    Add this to your page resource:

    <utils:ImageConverter x:Key="imageConverter" /> 


    Bind in the xaml to

     Source="{Binding file,Converter={StaticResource imageConverter}}" />  

    And add a new source which converts it over. Something like:

    class ImageConverter : IValueConverter   
      {   
        public object Convert(object value, Type targetType, object parameter, string language)   
        {   
          if (value.GetType() != typeof(StorageItemThumbnail))   
          {   
            throw new ArgumentException("Expected a thumbnail");   
          }   
          if (targetType != typeof(ImageSource))   
          {   
            throw new ArgumentException("What are you trying to convert to here?");   
          }   
          StorageItemThumbnail thumbnail = (StorageItemThumbnail)value;   
          BitmapImage image = new BitmapImage();   
          image.SetSource(thumbnail);   
          return (image);   
        }   
      
        public object ConvertBack(object value, Type targetType, object parameter, string language)   
        {   
          throw new NotImplementedException();   
        }   
      }  
    
    class ImageConverter : IValueConverter
      {
        public async object Convert(object value, Type targetType, object parameter, string language)
        {
          if (value.GetType() != typeof(StorageFile))
          {
            throw new ArgumentException("Expected a thumbnail");
          }
         
         // Grab the Thumbnail
        
          
          StorageItemThumbnail thumbnail = (StorageItemThumbnail) (await value.GetThumbnailAsync());
          BitmapImage image = new BitmapImage();
          image.SetSource(thumbnail);
          return (image);
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
          throw new NotImplementedException();
        }
      }
    

    This is just thrown together so I'm positive this would not be completely working code, but it should give you the shove to get what you need done.

    You can just step through changing what you need. :) A lot of this was snagged from http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2012/04/26/windows-8-metro-style-bits-of-binding.aspx

    Hope it helps!

    Saturday, March 01, 2014 12:42 AM

All replies

  • What are you databinding to? From where are you getting the album art?

    Friday, February 28, 2014 5:54 AM
  • from a file chosen by a file picker. this is my code for it

    Private Async Sub OpenAudio_Tapped(sender As Object, e As TappedRoutedEventArgs) Handles OpenAudio.Tapped
            Dim openpicker = New Windows.Storage.Pickers.FileOpenPicker()
            openpicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.MusicLibrary
            openpicker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail
    
            ' Filter File Types
            openpicker.FileTypeFilter.Clear()
            openpicker.FileTypeFilter.Add(".wma")
            openpicker.FileTypeFilter.Add(".mp3")
            openpicker.FileTypeFilter.Add(".avi")
    
            'Open file picker
    
            Dim file = Await openpicker.PickSingleFileAsync
    
            'file = null if user cancels
    
            If file IsNot Nothing Then
                ' open uup a stream for a selected file type
                Dim stream = Await file.OpenAsync(Windows.Storage.FileAccessMode.Read)
    
                'set image souce to selected bitmap image within the file open picker
                If file IsNot Nothing Then
                    Audioelement.SetSource(stream, file.ContentType)
    
                    Audioelement.Play()
                End If
    
                Me.DataContext = file
            End If
        End Sub

    Friday, February 28, 2014 6:51 PM
  • One options would be to use a converter. i.e.

    (For c#)

    Add this to your page resource:

    <utils:ImageConverter x:Key="imageConverter" /> 


    Bind in the xaml to

     Source="{Binding file,Converter={StaticResource imageConverter}}" />  

    And add a new source which converts it over. Something like:

    class ImageConverter : IValueConverter   
      {   
        public object Convert(object value, Type targetType, object parameter, string language)   
        {   
          if (value.GetType() != typeof(StorageItemThumbnail))   
          {   
            throw new ArgumentException("Expected a thumbnail");   
          }   
          if (targetType != typeof(ImageSource))   
          {   
            throw new ArgumentException("What are you trying to convert to here?");   
          }   
          StorageItemThumbnail thumbnail = (StorageItemThumbnail)value;   
          BitmapImage image = new BitmapImage();   
          image.SetSource(thumbnail);   
          return (image);   
        }   
      
        public object ConvertBack(object value, Type targetType, object parameter, string language)   
        {   
          throw new NotImplementedException();   
        }   
      }  
    
    class ImageConverter : IValueConverter
      {
        public async object Convert(object value, Type targetType, object parameter, string language)
        {
          if (value.GetType() != typeof(StorageFile))
          {
            throw new ArgumentException("Expected a thumbnail");
          }
         
         // Grab the Thumbnail
        
          
          StorageItemThumbnail thumbnail = (StorageItemThumbnail) (await value.GetThumbnailAsync());
          BitmapImage image = new BitmapImage();
          image.SetSource(thumbnail);
          return (image);
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
          throw new NotImplementedException();
        }
      }
    

    This is just thrown together so I'm positive this would not be completely working code, but it should give you the shove to get what you need done.

    You can just step through changing what you need. :) A lot of this was snagged from http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2012/04/26/windows-8-metro-style-bits-of-binding.aspx

    Hope it helps!

    Saturday, March 01, 2014 12:42 AM