Windows 8 DevShort: BindableBase (bye bye INotifyPropertyChanged)

With BUILD, Windows 8.1 and a Windows 8 API refresh right around the corner it’s time for another Win8 dev post. We’ll look at the BindableBase class which will make DataBinding a little bit easier.

The class isn’t officially part of the .NET4.5 specification, at least not today but it ships with every Windows 8 Metro app template you pick.

For databinding most people implement the INotifyPropertyChanged interface to notify UI when underlying data changes, this works in the Windows Runtime environment too, but it looks a bit “meh”.

Here’s a sample of our earlier post on WPF databinding basics:

 public class Employee : INotifyPropertyChanged
 {
 string name;
 public string Name
 {
 get { return name; }
 set
 {
 if (name != value)
 name = value; OnPropertyChanged("Name");
 }
 }

Role role;
 public Role Role
 {
 get { return role; }
 set
 {
 if (role != value)
 role = value; OnPropertyChanged("Role");
 }
 }

public event PropertyChangedEventHandler PropertyChanged;

void OnPropertyChanged(string propertyName)
 {
 if (PropertyChanged != null)
 PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
 }
 }

In this case of databinding we implement the INotifyPropertyChanged interface and use OnPropertyChanged function and event handler to push the data member changes to the UI. The BindableBase that ships in Windows 8 is a wrapper class that does this for you under the hood but makes it more straightforward for you.  The above class would be rewritten like this:

 public class Employee : INotifyPropertyChanged
 {
 string name;
 public string Name
 {
 get { return name; }
 set
 {
 if (name != value)
SetProperty(ref name, value);
 }
 }

Role role;
 public Role Role
 {
 get { return role; }
 set
 {
 if (role != value)
 SetProperty(ref role, value);
 }
 }



We see we don’t inherit from an interface anymore but from an actual class which provides the SetProperty function that sets the variable and notifies the UI. Notice that we don’t even have to specify a String to pass the Property’s name (which is huge!) , the BindableBase class uses the CallerMemberNameAttribute attribute (new in .NET 4.5) under the hood to determine the property  name, make sure to head over to MSDN and read that handy thing over.

As a quick reminder Build starts next week and MS has officially revealed people will get a preview version of Windows 8.1. Win8.1  contains numerous of great improvements and additions in less than a year after the original release of Windows 8, and it’s great to see Microsoft moving at such a fast pace. I can’t wait to see what Microsoft has in store for the Windows 8.1 Runtime, and Xbox & Windows Phone development. The current Windows 8 API is still premature sometimes, and the Store UI is borderline useless (something they have finally fixed).

I haven’t really been onboard Windows 8 development myself lately, but maybe 8.1 might make me hungry again. One of my tryout apps (Activity Runner) is up in the store, and I still have this big Twitter client lying around waiting to get finished 😉

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s