inicio mail me! sindicaci;ón

Silverlight 2.0 Flickr WordPress Widget

I had the challenge of creating both a simple Silverlight 2.0 flickr viewer and a WordPress widget that used silverlight so I combined the two projects in to one.

The first part was creating the Silverlight 2.0 Flickr RSS viewer.

   1: <UserControl
   2:     xmlns="http://schemas.microsoft.com/client/2007"
   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   4:     x:Class="FlickrShow.Page"
   5:     Width="auto" 
   6:     Height="auto" 
   7:     x:Name="FlickrShow" 
   8:     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
   9:     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"   
  10:     mc:Ignorable="d">
  11:  
  12:     <Grid x:Name="LayoutRoot" Background="#FF424242" >
  13:         <Image HorizontalAlignment="Stretch" Margin="0,0,0,0" x:Name="ImageItem" VerticalAlignment="Stretch" Stretch="Uniform" Cursor="Hand" Source="OpeningImage.jpg">
  14:             <Image.Resources>
  15:                 <Storyboard x:Name="FadeOutAnimation">
  16:                     <DoubleAnimation Duration="00:00:00.20" From="1" To="0"                        
  17:                                      Storyboard.TargetProperty="Opacity"                        
  18:                                      Storyboard.TargetName="BigImage" />
  19:                 </Storyboard>
  20:                 <Storyboard x:Name="FadeInAnimation">
  21:                     <DoubleAnimation Duration="00:00:00.20" From="0" To="1"                        
  22:                                      Storyboard.TargetProperty="Opacity"                        
  23:                                      Storyboard.TargetName="BigImage" />
  24:                 </Storyboard>
  25:             </Image.Resources>
  26:         </Image>
  27:         <TextBox Height="30" VerticalAlignment="Bottom" Text="" x:Name="LabelBox" Background="#2B000000" Foreground="#FFFFFFFF" BorderThickness="0,0,0,0" FontSize="11" />
  28:     </Grid>
  29: </UserControl>

page.xaml

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Windows;
   5: using System.Windows.Controls;
   6: using System.Windows.Documents;
   7: using System.Windows.Input;
   8: using System.Windows.Media;
   9: using System.Windows.Media.Animation;
  10: using System.Windows.Shapes;
  11: using System.Windows.Threading;
  12: using System.Windows.Browser;
  13: using System.Xml.Linq;
  14: using System.Windows.Media.Imaging;
  15: using System.Windows.Resources;
  16: using System.IO;
  17: using System.Net;
  18: using System.Xml;
  19: using System.ServiceModel.Syndication;
  20:  
  21:  
  22:  
  23: namespace FlickrShow
  24: {
  25:     public class SettingDefinition
  26:     {
  27:         public string userID { get; set; }
  28:         public string feedType  { get; set; }
  29:         public string tags  { get; set; }
  30:         public string duration { get; set; }
  31:     }
  32:  
  33:     public partial class Page : UserControl
  34:     {
  35:         string currentUserID;
  36:         string currentFeedType;
  37:         string currentTags;
  38:         int currentDuration;
  39:  
  40:         DateTime lastUpdate;
  41:         string currentLink;
  42:         String[,] imgArray = new String[20, 3]; //Array to store information pulled from the RSS
  43:         int i = 0;  //handy little index number to count with
  44:         int imageIndex = 0;  //another index used for displaying specific images
  45:         DispatcherTimer dt = new DispatcherTimer();
  46:         
  47:         public Page(string UserID, string FeedType, string Tags, int Duration)
  48:         {
  49:             InitializeComponent();
  50:  
  51:             currentUserID = UserID;
  52:             currentFeedType = GetFeedType(FeedType);
  53:             currentTags = Tags;
  54:             currentDuration = Duration;
  55:  
  56:             this.Loaded += new RoutedEventHandler(Page_Loaded);
  57:             HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri("http://api.flickr.com/services/feeds/" + currentFeedType + currentUserID + currentTags + "&format=rss2"));
  58:  
  59:             request.BeginGetResponse(new AsyncCallback(getImages), request);
  60:  
  61:             lastUpdate = DateTime.Now;
  62:  
  63:             dt.Interval = new TimeSpan(0, 0, 0, currentDuration);
  64:             dt.Tick += new EventHandler(dt_Tick);
  65:             dt.Start();
  66:  
  67:         }
  68:  
  69:         void getImages(IAsyncResult asyncResult)
  70:         {
  71:             XNamespace mediaNamespace = "http://search.yahoo.com/mrss/";
  72:  
  73:  
  74:             HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
  75:             HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);
  76:  
  77:             XmlReader reader = XmlReader.Create(response.GetResponseStream());
  78:             SyndicationFeed feed = SyndicationFeed.Load(reader);
  79:  
  80:             foreach (SyndicationItem item in feed.Items)
  81:             {
  82:                 //let's put the results in a multidimesional Array, because they are fun 
  83:                 imgArray.SetValue(item.ElementExtensions[1].GetReader().GetAttribute("url"), i, 0);
  84:                 imgArray.SetValue(item.Title.Text, i, 1);
  85:                 imgArray.SetValue("http://www.flickr.com" + item.Links[0].Uri.AbsolutePath, i, 2);
  86:                 i++;
  87:             }
  88:         }
  89:  
  90:         void Page_Loaded(object sender, RoutedEventArgs e)
  91:         {
  92:                       
  93:             ImageItem.MouseEnter += new MouseEventHandler(ImageItem_MouseEnter);
  94:             ImageItem.MouseLeave += new MouseEventHandler(ImageItem_MouseLeave);
  95:             ImageItem.MouseLeftButtonDown += new MouseButtonEventHandler(ImageItem_MouseButtonDown);
  96:  
  97:             
  98:         }
  99:  
 100:  
 101:  
 102:         void dt_Tick(object sender, EventArgs e)
 103:         {
 104:             //this is our timer control, every time we update this will run
 105:             Update();   
 106:         }
 107:  
 108:         void Update()
 109:         {
 110:             //Updating the time
 111:             DateTime now = DateTime.Now;
 112:             TimeSpan elapsed = now - lastUpdate;
 113:             lastUpdate = now;
 114:             //do your loop processing here
 115:             ChangeImage();
 116:             
 117:         }
 118:  
 119:         void ChangeImage()
 120:         {
 121:             //This is where we actually update the <image> XAML. 
 122:  
 123:             if (imageIndex <= (i - 1))
 124:             {
 125:                 LabelBox.Text = imgArray.GetValue(imageIndex, 1) as string;
 126:                 ImageItem.SetValue(Image.SourceProperty, imgArray.GetValue(imageIndex, 0) as string);
 127:                 currentLink = imgArray.GetValue(imageIndex, 2) as string;
 128:                 imageIndex++;
 129:             }
 130:             else
 131:             {
 132:                 imageIndex = 0;
 133:                 LabelBox.Text = imgArray.GetValue(imageIndex, 1) as string;
 134:                 ImageItem.SetValue(Image.SourceProperty, imgArray.GetValue(imageIndex, 0) as string);
 135:                 currentLink = imgArray.GetValue(imageIndex, 2) as string;
 136:                 imageIndex++;
 137:             }
 138:  
 139:         }
 140:  
 141:         public string GetFeedType(string feedSetting)
 142:         {
 143:             switch (feedSetting)
 144:                 {
 145:                 case "Public":
 146:                         return "photos_public.gne?id=";                        
 147:                 case "Friends":
 148:                     //This is for a future release
 149:                         return "photos_friends.gne?user_id=";
 150:                 default:
 151:                         return "photos_public.gne?id="; 
 152:                 }
 153:             
 154:         }
 155:  
 156:         public string FormatTags(string tagsElement)
 157:         {
 158:             if (tagsElement.Length > 0)
 159:             {
 160:                 return "&tags=" + tagsElement;
 161:             }
 162:             else { return ""; }
 163:         }
 164:  
 165:         private void ImageItem_MouseEnter(object sender, MouseEventArgs e)
 166:         {            
 167:                 dt.Stop();
 168:         }
 169:  
 170:         private void ImageItem_MouseLeave(object sender, MouseEventArgs e)
 171:         {
 172:             dt.Start();
 173:         }
 174:  
 175:         private void ImageItem_MouseButtonDown(object sender, MouseButtonEventArgs e)
 176:         {
 177:             HtmlPage.Window.Navigate(new Uri(currentLink, UriKind.Absolute), "_flickr");
 178:         }
 179:     }
 180: }

page.xaml.cs

   1: using System.Windows;
   2: using System;
   3:  
   4: namespace FlickrShow
   5: {
   6:     public partial class App : Application 
   7:     {
   8:  
   9:         public App() 
  10:         {
  11:             this.Startup += this.OnStartup;
  12:             this.Exit += this.OnExit;
  13:  
  14:             InitializeComponent();
  15:         }
  16:  
  17:         private void OnStartup(object sender, StartupEventArgs e) 
  18:         {
  19:             // Load the main control here
  20:             string currentUserID = e.InitParams["flickr_userID"];
  21:             string currentFeedType = "Public";
  22:             string currentTags = "";
  23:             int currentDuration = Convert.ToInt32(e.InitParams["time"]);
  24:  
  25:             this.RootVisual = new Page(currentUserID, currentFeedType, currentTags, currentDuration);
  26:         }
  27:  
  28:         private void OnExit(object sender, EventArgs e)