Facebook C# SDK - and Silverlight 5
(This feature requires Facebook C# SDK v5.4 at minimum)
Now that Silverlight 5 has hit the RTW build, it is now time to officially release Facebook C# SDK build for Silverlight 5. Starting with Facebook C# SDK v5.4 we will be shipping the compiled libraries for Silverlight 5 (We will continue to support Silverlight 4 for few upcoming months, and would highly recommend you to update to Silverlight 5 if possible as you can take advantage of some new stuffs that has been baked in to take advantage of Silverlight 5).
With v5.3 we introduced support for Task Parallel Library (TPL) in .net 4.0 builds, and now with v5.4 we are now bringing it to Silverlight 5. You can read in details about TPL features in Facebook C# SDK at What's new in v5.3. This post will be specific to Silverlight 5.
A new sample called “CS-SL5-OutOfBrowser.sln” has been added which demonstrates the use of dyanmic and TPL with Silverlight 5.
Here is a sample graph api request under Silverlight 4:
var fb = new FacebookClient(_accessToken);
fb.GetCompleted += (o, e) =>
{
if (e.Error == null)
{
// make sure to reference Microsoft.CSharp if you want to use dynamic
dynamic me = e.GetResultData();
Dispatcher.BeginInvoke(
() =>
{
picProfile.Source = new BitmapImage(new Uri(string.Format("https://graph.facebook.com/{0}/picture", me.id)));
ProfileName.Text = "Hi " + me.name;
FirstName.Text = "First Name: " + me.first_name;
LastName.Text = "Last Name: " + me.last_name;
});
}
else
{
Dispatcher.BeginInvoke(() => MessageBox.Show(e.Error.Message));
}
};
fb.GetAsync("me");
Here is the same code in Silverlight 5 using Task Parallel Library. (the above code will continue to work in Silverlight 5 without any changes to your existing Silverlight 4 code base.)
private TaskScheduler _ui;
private FacebookClient _fb;
public MainPage()
{
InitializeComponent();
_ui = TaskScheduler.FromCurrentSynchronizationContext();
_fb = new FacebookClient("access_token");
WebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp);
}
private void GraphApiGetExample()
{
_fb.GetTaskAsync("me").ContinueWith(
t =>
{
if (t.IsFaulted)
{
MessageBox.Show(t.Exception.GetBaseException().Message);
return;
}
dynamic me = t.Result;
picProfile.Source = new BitmapImage(new Uri(string.Format("https://graph.facebook.com/{0}/picture", me.id)));
ProfileName.Text = "Hi " + me.name;
FirstName.Text = "First Name: " + me.first_name;
LastName.Text = "Last Name: " + me.last_name;
}, _ui);
}
We have added XTaskAsync methods in FacebookClient and FacebookOAuthClient so you can start taking advantage of TPL in silverlight 5. In this example we are making using of TaskScheduler so we can get rid of Dispatcher.BeginInvoke method. These will make your code cleaner and more maintainable. Another advantage of using TPL is that you can call multiple XTaskAsync methods on the same FacebookClient instance and it will call the callback (ContinueWith) correctly.
There are bunch of goodness in using Facebook Client with TPL, but rather then repeating I would recommend you to read my previous post on What’s New in v5.3 which talks in details about TPL in Facebook C# SDK.