johnllao

June 28, 2010

Simple Facebook WPF Application

Filed under: Uncategorized — johnllao @ 8:52 pm

On this blog I will show how to create a simple WPF application that connects to Facebook. In this sample it is assumed that you already acquired an Application ID from Facebook. If not please check this link http://developers.facebook.com/docs/.  The Application ID is one of the requirements to connect to Facebook.

To begin, download the Facebook toolkit from the this link http://facebooktoolkit.codeplex.com/ 

In you WPF application reference the following dlls:

  • Facebook.dll

Create the following class in your application:

/// <summary>
/// 
/// </summary>
public static class FacebookService
{
    private static DesktopSession _Session;
    /// <summary>
    /// 
    /// </summary>
    public static DesktopSession Session
    {
        get { return _Session; }
        set { _Session = value; }
    }
    private static BindingManager _Service;
    /// <summary>
    /// 
    /// </summary>
    public static BindingManager Service
    {
        get { return _Service; }
        set { _Service = value; }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="session"></param>
    public static void Init(DesktopSession session)
    {
        _Session = session;
        _Service = BindingManager.CreateInstance(_Session);
    }
}

The DesktopSession is a Facebook component from the toolkit that manage the connection and authentication to Facebook. To know more on how Facebook authenticates you application, check this link http://wiki.developers.facebook.com/index.php/How_Facebook_Authenticates_Your_Application

The BindingManager is another Facebook component that is reponsible retrieving friends list, photos and other information stored in a user’s profile.

To make this work, add the following code from the constructor of your WPF window:

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    private FacebookModel _vm;
    /// <summary>
    /// 
    /// </summary>
    public MainWindow()
    {
        DesktopSession session = new DesktopSession("cd0ade267315e76b6e9d0b5855df7e98", true);
        session.Login();
        FacebookService.Init(session);

        InitializeComponent();

        _vm = new FacebookModel();
        Loaded += new RoutedEventHandler(MainWindow_Loaded);
        Closing += new System.ComponentModel.CancelEventHandler(MainWindow_Closing);
    }

From the above code we use the DesktopSession to authenticate our applicatio to Facebook, when launching this application WPF will automatically redirects the application to a Facebook login page then returns back to our main window.

The following code shows the Facebook View Model that facilitates the retrieval of data from Facebook.

using Facebook.BindingHelper;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace MyFacebook.MainApplication.Model
{
    /// <summary>
    /// 
    /// </summary>
    public class FacebookModel : INotifyPropertyChanged
    {
        /// <summary>
        /// 
        /// </summary>
        public FacebookModel()
        {
            _Friends = FacebookService.Service.Friends;
            _Friends.DataRetrievalCompletedEvent += new System.EventHandler<DataRetrievalCompletedEventArgs>(Friends_DataRetrievalCompletedEvent);

            _Activities = FacebookService.Service.Stream;
            _Activities.DataRetrievalCompletedEvent += new System.EventHandler<DataRetrievalCompletedEventArgs>(Activities_DataRetrievalCompletedEvent);

            long user_id = FacebookService.Service.CurrentUserId;
            _CurrentUserList = FacebookService.Service.GetUsers(new long[] { user_id });
            _CurrentUserList.DataRetrievalCompletedEvent += new System.EventHandler<DataRetrievalCompletedEventArgs>(CurrentUser_DataRetrievalCompletedEvent);
        }

        #region Current User
        private FacebookContactCollection _CurrentUserList;
        private FacebookContact _CurrentUser;
        /// <summary>
        /// 
        /// </summary>
        public FacebookContact CurrentUser
        {
            get { return _CurrentUser; }
            set
            {
                if (_CurrentUser != value)
                {
                    _CurrentUser = value;
                    OnPropertyChanged("CurrentUser");
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CurrentUser_DataRetrievalCompletedEvent(object sender, DataRetrievalCompletedEventArgs e)
        {
            _CurrentUserList = sender as FacebookContactCollection;
            if (_CurrentUserList != null && _CurrentUserList.Count > 0)
            {
                CurrentUser = _CurrentUserList[0];
            }
        }
        #endregion

        #region Activities
        private ActivityPostCollection _Activities;
        /// <summary>
        /// 
        /// </summary>
        public ActivityPostCollection Activities
        {
            get { return _Activities; }
            set
            {
                if (_Activities != value)
                {
                    _Activities = value;
                    OnPropertyChanged("Activities");
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Activities_DataRetrievalCompletedEvent(object sender, DataRetrievalCompletedEventArgs e)
        {

        }
        #endregion

        #region Selected Friend
        private FacebookContact _SelectedFriend;
        /// <summary>
        /// 
        /// </summary>
        public FacebookContact SelectedFriend
        {
            get { return _SelectedFriend; }
            set
            {
                if (_SelectedFriend != value)
                {
                    _SelectedFriend = value;
                    SelectedFriendPhotos = _SelectedFriend.PhotosBy;
                    OnPropertyChanged("SelectedFriend");
                }
            }
        }

        private FacebookPhotoCollection _SelectedFriendPhotos;
        /// <summary>
        /// 
        /// </summary>
        public FacebookPhotoCollection SelectedFriendPhotos
        {
            get { return _SelectedFriendPhotos; }
            set
            {
                if (_SelectedFriendPhotos != value)
                {
                    _SelectedFriendPhotos = value;
                    OnPropertyChanged("SelectedFriendPhotos");
                }
            }
        }
        #endregion

        #region Friends
        private FacebookContactCollection _Friends;
        /// <summary>
        /// 
        /// </summary>
        public FacebookContactCollection Friends
        {
            get
            {
                return _Friends;
            }
            set
            {
                if (_Friends != value)
                {
                    _Friends = value;
                    OnPropertyChanged("Friends");
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Friends_DataRetrievalCompletedEvent(object sender, DataRetrievalCompletedEventArgs e)
        {

        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="propertyName"></param>
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

That’s it!

Advertisement

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

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 )

Connecting to %s

Theme: Shocking Blue Green. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.