App in a snap: Feeds only show a single item

As you may have seen in my App in a snap: Your first Windows 8 app blog post, here at Microsoft The Netherlands we’ve created Windows 8 App in a snap to allow developers and non-developers to create their first Windows 8 app with relative ease. App in a snap uses RSS feeds as content for the app, but unfortunately there are many variants of RSS to take into account.

We’ve tried our best to cover most of the spectrum, but sometimes you can come across a feed that doesn’t seem to work properly with App in a snap. One of those cases is where the feeds only show a single item in the app, which is most likely caused by the fact that the items don’t have an id, which we use to determine whether or not there are new items to add to the list when refreshing the content. Luckily, the fix for this problem is quite easy to implement!

Change the identifier

To keep things short, we use the id property of the item to check whether or not the item exists in the current list of retrieved items. If it does, we simply skip over the item and don’t add it again. If the id property is missing, logic always assumes the item exists and skips over the item. To change this behavior, open the UpdateFromSource.cs file in the Behaviors folder and find the following code starting on line 26:

if (feed.ContainsItemWithId(syndicationItem.Id)
{
    continue;
}

Since we don’t have an identifier in the feed, we need to use a different property to identify the feed. Here’s where some fuzzy logic comes in to determine the item’s identity: we’ll first try to use the ItemUri property of the item, which holds the Uri of the item. If that doesn’t exist either (highly unlikely), we’ll use the Title property of the item. Keep in mind that these could both theoretically change, which would result in the item showing up twice when refreshing the data. Replace the four lines of code outlined above with the following:

var id = syndicationItem.Id;
if (string.IsNullOrWhiteSpace(id) && syndicationItem.ItemUri != null)
{
    id = syndicationItem.ItemUri.ToString();
}
if (string.IsNullOrWhiteSpace(id) && syndicationItem.Title != null)
{
    id = HtmlUtilities.ExtractString(syndicationItem.Title.Text);
}

if (feed.ContainsItemWithId(id))
{
    continue;
}

Lastly, we need to make sure that the new id is used as the actual item id, so it gets passed properly when navigating. Change the following line in the try{} block:

item.Id = syndicationItem.Id;

to

item.Id = id;

Now when you try your feed that previously only showed one item, all items should show up and the refresh button from the app bar shouldn’t cause duplicate items to be added!

Leave a Reply