FaceBook Service Into UWP App Using UWP Community Toolkit

Introduction

This article is divided into three sections, the first two sections explain about the installation of the Facebook application and install the UWP service tool kit and final part explains integration into the application,

  1. Install the UWP toolkit
  2. Create Facebook Service
  3. Integrate Facebook into main application

Install the UWP Toolkit

UWP Community Toolkit is a collection of helper functions, custom controls, and app services. It simplifies and demonstrates common developer tasks building UWP apps.

More info available here.

For our application to implement face book integration ,there is no need to write the code from scratch, the UWP toolkit helper class is available, we just add the helper reference into our application.

Adding helper reference into our project.

Steps

  1. Create a new project (ex: FacebookService)
  2. right click -> Manage NuGet Packages
  3. Search “Microsoft.Toolkit. Uwp”
  4. Select the “Microsoft.Toolkit. Uwp. Services” & Install

project

UWP Tool kit has installed into our application.

Create Facebook Service

We have to create a Facebook Service App to interact with our application into Facebook, first we will see how to create Facebook Service

Note - Create Facebook Service -- first we need our application Windows store App Id, let us create a Windows store App Id

  1. Create a new Universal Windows Program project (ex: Facebook Service).
  2. Call FacebookService.Instance. WindowsStoreId, it generates the Windows Store Id.

Steps

  1. Goto the site & Register,

    Register

  2. Create a App Id,

    http://www.c-sharpcorner.com/article/facebook-service-into-uwp-app-using-uwp-community-toolkit/

  3. Goto Settings -> Basic -> Add Platform -> Select the Windows App,

    http://www.c-sharpcorner.com/article/facebook-service-into-uwp-app-using-uwp-community-toolkit/

  4. Enter the Windows Store SID which we have created in the Note section,

    http://www.c-sharpcorner.com/article/facebook-service-into-uwp-app-using-uwp-community-toolkit/

  5. Add product & Login, select below option has enable or not, if not please enable it,

    http://www.c-sharpcorner.com/article/facebook-service-into-uwp-app-using-uwp-community-toolkit/

Facebook service application has been created.

Integrated Facebook service into main application

Add the namespace and initialize the service (same project which has created in the previous section)
  1. using Microsoft.Toolkit.Uwp.Services.Facebook.
  2. FacebookService.Instance.Initialize(FaceBookAddId).

To Initialize the Facebook service , Facebook App Id is required , this App Id is available in the Facebook service application which we have created in the previous section,



First time we Initialize the service (running the app) a Facebook login and password is required. Enter the login and password to connect the facebook service,

http://www.c-sharpcorner.com/Publish/CreateArticle.aspx?ArticleID=5d7f5908-903f-41b0-a1b1-2c752d34decf&PagePath=/article/facebook-service-into-uwp-app-using-uwp-community-toolkit/default.aspx

Below is the sample Code to Initialize fb Service and load the Tagged information

  1. FacebookService.Instance.GetUserPictureInfoAsync(); get facebook profile picture
  2. FacebookService.Instance.RequestAsync(FacebookDataConfig.MyTagged, 10); -> Facebook tagged information

FacebookDataConfig class is use to get the user Feed , Posts , Tagged information,

  1. private void LoadFaceBookService() {  
  2.         FacebookService.Instance.WindowsStoreId;  
  3.         FacebookService.Instance.Initialize(FaceBookAddId);  
  4.         LoadFaceBookInfo();  
  5.     }  
  6.     .  
  7. public ObservableCollection < FaceBookInfo > Fbcollection = new ObservableCollection < FaceBookInfo > ();  
  8.   
  9. private async void LoadFaceBookInfo() {  
  10.     if (!await FacebookService.Instance.LoginAsync()) {  
  11.         return;  
  12.     }  
  13.   
  14.     var fbItem = await FacebookService.Instance.GetUserPictureInfoAsync();  
  15.     ImageSource imgSource = new BitmapImage(new Uri(fbItem.Url));  
  16.     Img.Source = imgSource;  
  17.     await LoadFbItems();  
  18.   
  19. }  
  20.   
  21. private async Task LoadFbItems() {  
  22.     var lstFbItems = await FacebookService.Instance.RequestAsync(FacebookDataConfig.MyTagged, 10);  
  23.     Fbcollection = new ObservableCollection < FaceBookInfo > ();  
  24.   
  25.     foreach(var eachItem in lstFbItems) {  
  26.         var fbItem = new FaceBookInfo {  
  27.             TextInfo = eachItem.Message,  
  28.                 CreateDate = eachItem.Created_Time.ToString(CultureInfo.InvariantCulture)  
  29.         };  
  30.         Fbcollection.Add(fbItem);  
  31.     }  
  32.   
  33.     LstView.ItemsSource = Fbcollection;  
  34.   
  35. }  
Sample output

output

Post the Tag from our Application

FacebookService.Instance.PostToFeedWithDialogAsync -> This function is use to post the information in our facebook page via facebook dialog,
  1. private async void BtnNewPost_OnClick(object sender, RoutedEventArgs e)   
  2. {   
  3. await FacebookService.Instance.PostToFeedWithDialogAsync("UWP Facebook Service", "PostToFeedWithDialog",   
  4. "http://www.c-sharpcorner.com/article/twitter-service-into-uwp-app-using-uwp-community-toolkit/");   
  5. }   
private async void BtnNewPost_OnClick(object sender, RoutedEventArgs e) { await FacebookService.Instance.PostToFeedWithDialogAsync("UWP Facebook Service", "PostToFeedWithDialog", "http://www.c-sharpcorner.com/article/twitter-service-into-uwp-app-using-uwp-community-toolkit/"); }

Xaml code sample
  1. <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
  2.     <Grid.RowDefinitions>  
  3.         <RowDefinition Height="25*" />  
  4.         <RowDefinition Height="75*" />  
  5.     </Grid.RowDefinitions>  
  6.     <Grid>  
  7.         <Grid.ColumnDefinitions>  
  8.             <ColumnDefinition/>  
  9.             <ColumnDefinition/>  
  10.         </Grid.ColumnDefinitions>  
  11.         <StackPanel>  
  12.             <StackPanel>  
  13.                 <Image x:Name="Img" Stretch="Uniform" Width="100" Height="100"></Image>  
  14.                 <Button x:Name="BtnNewPost" Content="New Post" Click="BtnNewPost_OnClick"></Button>  
  15.             </StackPanel>  
  16.         </StackPanel>  
  17.     </Grid>  
  18.   
  19.     <ListView x:Name="LstView" Grid.Row="1" Margin="10">  
  20.         <ListView.ItemTemplate>  
  21.             <DataTemplate>  
  22.                 <Grid>  
  23.                     <Grid.ColumnDefinitions>  
  24.                         <ColumnDefinition Width="Auto" />  
  25.                         <ColumnDefinition Width="Auto" />  
  26.                     </Grid.ColumnDefinitions>  
  27.                     <StackPanel Grid.Column="1">  
  28.                         <TextBlock Text="{Binding TextInfo}" />  
  29.                         <TextBlock Text="{Binding CreateDate}" Foreground="Blue" FontWeight="Bold" />  
  30.                     </StackPanel>  
  31.                 </Grid>  
  32.             </DataTemplate>  
  33.         </ListView.ItemTemplate>  
  34.     </ListView>  
  35.     </Grid>
Our FacebookInfo define class
  1. public class FaceBookInfo: INotifyPropertyChanged {  
  2.     public string TextInfo {  
  3.         get {  
  4.             return _textInfo;  
  5.         }  
  6.         set {  
  7.             _textInfo = value;  
  8.             OnPropertyChanged();  
  9.         }  
  10.     }  
  11.   
  12.     private string _textInfo;  
  13.   
  14.     public string ImgSource {  
  15.         get {  
  16.             return _imgSource;  
  17.         }  
  18.         set {  
  19.             _imgSource = value;  
  20.             OnPropertyChanged();  
  21.         }  
  22.     }  
  23.   
  24.     private string _imgSource;  
  25.   
  26.     private string _createDate;  
  27.   
  28.     public string CreateDate {  
  29.         get {  
  30.             return _createDate;  
  31.         }  
  32.         set {  
  33.             createDate = value;  
  34.             OnPropertyChanged();  
  35.         }  
  36.     }  
  37.   
  38.   
  39.     public event PropertyChangedEventHandler PropertyChanged;  
  40.   
  41.     protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) {  
  42.         PropertyChanged ? .Invoke(this, new PropertyChangedEventArgs(propertyName));  
  43.     }  
  44. }  
Above code is just an example of how to integrate Facebook into our application. Hope you enjoy and understand it.


Similar Articles