Building an Open Data App – Part 5: Updating the Bindings

Last time I showed you how to get your data onto user’s screens without hardcoding it. Everything you reload the control, or the user returns to the page, you’ll see the updated data. But, what if the user doesn’t leave the screen? What if you want to update with new data without updating the control? That’s what I’m going to show you today.

The simplest way to have a collection update is by using an ObservableCollection. Using our existing app, that would mean turning the IEnumerable<BikeStation> BikeStations into an ObservableCollection<BikeStation>. You’ll also need to update the BikeStationResponse class as well. Finally, in MainDataModel.GetStations() you’ll have to replace the line that sets BikeStations with the following:

BikeStations = new ObservableCollection<BikeStation>(bikeStationsResponse.Stations);

ObservableCollections have built in properties that, when the collection or it’s items change, inform the UI code of the need to reload. This precludes the developer from having to do anything. Now, you may ask yourself, that’s fine and grand for Collections, but what do I do about non-collections? What if I want to update a single string or int with high score data or such? That’s where INotifyPropertyChanged comes in.

The first step is to have the class inherit INotifyPropertyChanged. Next, you’ll need to implement the interface and add a bit of code to handle the actual updating. Those methods will look like this:

public event PropertyChangedEventHandler PropertyChanged;

protected void OnPropertyChanged(string propertyName = null)
    var eventHandler = this.PropertyChanged;
    if (eventHandler != null)
        eventHandler(this, new PropertyChangedEventArgs(propertyName));

What this does is when you update a property you’ll call OnPropertyChanged with an argument of the property name as a string, such as this OnPropertyChanged("BikeStations"). This will cause any reliant bindings to update. So, going to our example we’d implement this thusly:

    var bikeStationsResponse = JsonConvert.DeserializeObject<BikeStation>(jsonString);
    BikeStations = new ObservableCollection<BikeStation>(bikeStationsResponse.Stations);
catch (Exception e)
    //Code to handle the exception

You may wonder why I’m using this with ObservableCollection, as they already handle this. The reason is this: To activate the ObservableCollections update you need to affect the collection. Changing the contents of an object in the collection won’t cause the update.With that, you have the skeleton for an app that is built using a single open data dataset. Next, we’ll implement an additional feature or two.

This entry was posted in .NET, Building an Open Data App, Open Data, Windows 8, Windows 8.x, Windows Phone and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s