Over the past few months, I have been working on a Silverlight kit for Photobucket's API. I have used this sample to demonstrate a few things:
Now, I'd like to show how to create a CLR object that can expose the API so that designers can start using the API in Expression Blend without touching code. To see the end results, take a look at this Sketchbook demo and see the listbox that shows a list of images, in this case using the Photobucket Get Featured Media API.
Here's what my classes looks like:
using System; using System.ComponentModel; using System.Collections.Generic; using System.Linq; namespace Photobucket { /// <summary> /// Photobucket library for CLR Binding /// </summary> public class PhotobucketLibrary : INotifyPropertyChanged { #region Member Data IEnumerable<Media> m_featuredMedia; #endregion #region Properties /// <summary> /// Get/set the featured media /// </summary> [Category("Media")] public IEnumerable<Media> FeaturedMedia { get { if (m_featuredMedia == null) { API api = new API(); api.GetFeaturedMedia(new EventHandler<Photobucket.SearchCompletedEventArgs>(GotFeaturedMedia)); } return m_featuredMedia; } set { m_featuredMedia = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("FeaturedMedia")); } } } #endregion #region INotifyPropertyChanged Members /// <summary> /// Event triggered when properties change /// </summary> public event PropertyChangedEventHandler PropertyChanged; #endregion #region Implementation void GotFeaturedMedia(object sender, SearchCompletedEventArgs args) { FeaturedMedia = args.PrimaryMedia; } #endregion } }
using System; using System.Text; namespace Photobucket { /// <summary> /// Photobucket Media /// </summary> public class Media { /// <summary> /// Media Description ID /// </summary> public string DescriptionId { get; set; } /// <summary> /// Media Name /// </summary> public string Name { get; set; } /// <summary> /// Public /// </summary> public int Public { get; set; } /// <summary> /// Media Type /// </summary> public string MediaType { get; set; } /// <summary> /// User Name /// </summary> public string UserName { get; set; } /// <summary> /// Browse URL /// </summary> public Uri BrowseUrl { get; set; } /// <summary> /// Image URL /// </summary> public Uri Url { get; set; } /// <summary> /// Album URL /// </summary> public Uri AlbumUrl { get; set; } /// <summary> /// Thumbnail image /// </summary> public Uri Thumb { get; set; } /// <summary> /// Description /// </summary> public string Description { get; set; } /// <summary> /// Media Title /// </summary> public string Title { get; set; } /// <summary> /// Upload date /// </summary> public DateTime UploadDate { get; set; } /// <summary> /// Output the property names and values /// </summary> /// <returns></returns> public override string ToString() { return GetProperties(this); } static string GetProperties(object data) { var type = data.GetType(); System.Reflection.PropertyInfo[] properties = type.GetProperties(); StringBuilder builder = new StringBuilder(type.FullName); foreach (var property in properties) { builder.AppendLine(); builder.AppendFormat(System.Globalization.CultureInfo.CurrentCulture, "{0}: {1}", property.Name, property.GetValue(data, null)); } return builder.ToString(); } } }
Now that I have a class that exposes the property, I'd want to use it in Expression Blend 2.5 to add to a Silverlight 2 project. The class that I created is now part of the Silverlight assembly, so I can use it as a CLR Data Source:
From this demonstration, hopefully I was able to show a workflow for how a designer and a developer could work together using Blend and Visual Studio: in my case those two roles occupy the two opposite sides of my brain.