/// <summary> /// Method for retrieving RSS feed information, especially the image of the feed /// </summary> /// <param name="feed">Feed whose information to fetch</param> /// <param name="onGetRSSFeedImageUriCompleted">Action to take when information has been retrieved</param> /// <param name="onError">Action to take when an error occurs</param> public static void GetRSSFeedImageUri(RSSFeed feed, Action <Uri, RSSFeed> onGetRSSFeedImageUriCompleted = null, Action <Exception> onError = null) { WebClient webClient = new WebClient(); webClient.OpenReadCompleted += delegate(object sender, OpenReadCompletedEventArgs e) { try { if (e.Error != null) { if (onError != null) { onError(e.Error); } return; } XmlReader response = XmlReader.Create(e.Result); SyndicationFeed rssFeed = SyndicationFeed.Load(response); if (onGetRSSFeedImageUriCompleted != null) { onGetRSSFeedImageUriCompleted(rssFeed.ImageUrl, feed); } } catch (Exception error) { if (onError != null) { onError(error); } } }; webClient.OpenReadAsync(new Uri(feed.URL)); }
/// <summary> /// Constructor /// </summary> /// <param name="title">Title of the item</param> /// <param name="text">Summary of the item</param> /// <param name="url">URL of the item</param> /// <param name="date">Datestamp of the item</param> /// <param name="feed">Feed information</param> /// <param name="image">Image associated with the item</param> public RSSItem(String title, String text, String url, DateTimeOffset date, RSSFeed feed, string image) { Title = title; Text = text; Url = url; Datestamp = date; Feed = feed; Image = image; }
/// <summary> /// Returns a RSS feed from cache based on page id and feed id /// </summary> /// <param name="pageId">Id of the page where the feed resides</param> /// <param name="feedId">Id of the feed</param> /// <returns>RSS feed</returns> public static RSSFeed GetRSSFeed(int pageId, int feedId) { RSSFeed feed = null; int count = 0; foreach (RSSFeed f in CachedItems.Cache[pageId].Feeds) { if (!f.IsVisible) { continue; } else { if (count == feedId) { feed = f; break; } count++; } } return(feed); }
/// <summary> /// Method for retrieving RSS feed information, especially the image of the feed /// </summary> /// <param name="feed">Feed whose information to fetch</param> /// <param name="onGetRSSFeedImageUriCompleted">Action to take when information has been retrieved</param> /// <param name="onError">Action to take when an error occurs</param> public static void GetRSSFeedImageUri(RSSFeed feed, Action<Uri, RSSFeed> onGetRSSFeedImageUriCompleted = null, Action<Exception> onError = null) { WebClient webClient = new WebClient(); webClient.OpenReadCompleted += delegate(object sender, OpenReadCompletedEventArgs e) { try { if (e.Error != null) { if (onError != null) { onError(e.Error); } return; } XmlReader response = XmlReader.Create(e.Result); SyndicationFeed rssFeed = SyndicationFeed.Load(response); if (onGetRSSFeedImageUriCompleted != null) { onGetRSSFeedImageUriCompleted(rssFeed.ImageUrl, feed); } } catch (Exception error) { if (onError != null) { onError(error); } } }; webClient.OpenReadAsync(new Uri(feed.URL)); }
/// <summary> /// Method that initializes a single pivot item /// </summary> /// <param name="f">Feed to initialize the pivot item from</param> private void CreatePivotItem(RSSFeed f) { if (f.IsVisible) { // Programmatically create the pivot item // Note that most of the time you'd want to do this with XAML and data binding, // or at least use a data template for the content of the pivot item. However in // this case it's better to create everything manually because we are updating the state // of pivot items in LoadItems(). This means that we have to access the pivot // item controls directly, which is very difficult to do in Silverlight if the // controls are being created through data template (this is because FindName() // method of FrameworkTemplate is not available in Silverlight). It would probably be // possible to go around the need of accessing controls directly through data-binding but // that would most likely lead to more code lines than the 16 we need here. PivotItem pivotItem = new PivotItem(); pivotItem.Header = f.Title; pivotItem.Margin = new Thickness(0, 0, 0, 0); Grid grid = new Grid(); grid.Margin = new Thickness(25, 0, 0, 0); // Add a progressbar for each pivot item, initially hidden ProgressBar progressBar = new ProgressBar(); progressBar.IsIndeterminate = false; progressBar.Visibility = Visibility.Collapsed; grid.Children.Add(progressBar); ListBox listBox = new ListBox(); listBox.ItemTemplate = (DataTemplate)Resources["RSSItemTemplate"]; listBox.SelectionChanged += ItemSelectionChanged; // Enable tilt effect for the listbox. // TilEffect is part of Silverlight Toolkit which does not ship as part of this example. Please see // release notes for instructions how to install and use Silverlight Toolkit. TiltEffect.SetIsTiltEnabled(listBox, true); grid.Children.Add(listBox); pivotItem.Content = grid; RSSItemsPivot.Items.Add(pivotItem); } }
/// <summary> /// Gets the RSS items /// </summary> /// <param name="feed">The RSS feed</param> /// <param name="onGetRSSItemsCompleted">Callback on complete</param> /// <param name="onError">Callback for errors</param> public static void GetRSSItems(int pageId, int feedId, bool useCache, Action <IEnumerable <RSSItem> > onGetRSSItemsCompleted = null, Action <Exception> onError = null) { DateTime validUntilThis = DateTime.Now; validUntilThis = validUntilThis.AddMinutes(-EXPIRE_TIME); RSSFeed feed = GetRSSFeed(pageId, feedId); bool feedExists = (feed != null); bool feedHasItems = (feedExists && feed.Items != null && feed.Items.Count > 0); bool cacheHasExpired = (DateTime.Compare(validUntilThis, feed.Timestamp) > 0); // First check if this valid items for this feed exist in the cache already if (feedExists && feedHasItems && useCache && !cacheHasExpired && onGetRSSItemsCompleted != null) { onGetRSSItemsCompleted(feed.Items); } // Items not found in cache, perform a web request else { WebClient webClient = new WebClient(); webClient.OpenReadCompleted += delegate(object sender, OpenReadCompletedEventArgs e) { try { if (e.Error != null) { if (onError != null) { onError(e.Error); } return; } List <RSSItem> rssItems = new List <RSSItem>(); XmlReader response = XmlReader.Create(e.Result); SyndicationFeed rssFeed = SyndicationFeed.Load(response); foreach (SyndicationItem syndicationItem in rssFeed.Items) { // Clean the title in case it includes line breaks string title = syndicationItem.Title.Text; title = title.Replace("\r\n", "\n").Replace("\r", "\n").Replace("\n", " "); // Check for "enclosure" tag that references an image string image = ""; foreach (SyndicationLink link in syndicationItem.Links) { if (link.RelationshipType == "enclosure" && link.MediaType.StartsWith("image/")) { image = link.Uri.ToString(); break; } } RSSItem rssItem = new RSSItem( title, syndicationItem.Summary.Text, syndicationItem.Links[0].Uri.AbsoluteUri, syndicationItem.PublishDate, feed, image); rssItems.Add(rssItem); } // Cache the results feed.Items = rssItems; feed.Timestamp = DateTime.Now; // Call the callback with the list of RSSItems if (onGetRSSItemsCompleted != null) { onGetRSSItemsCompleted(rssItems); } } catch (Exception error) { if (onError != null) { onError(error); } } }; webClient.OpenReadAsync(new Uri(feed.URL)); } }