Permalink
Browse files

graph api sample

  • Loading branch information...
1 parent 1737a07 commit 982472016ec9dcc2651dd6174da44e30c3ae0025 @prabirshrestha prabirshrestha committed Apr 28, 2012
@@ -11,7 +11,8 @@
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
- shell:SystemTray.IsVisible="True">
+ shell:SystemTray.IsVisible="True"
+ Loaded="PhoneApplicationPage_Loaded">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
@@ -22,14 +23,41 @@
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
- <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
- <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
+ <TextBlock x:Name="ApplicationTitle" Text="Facebook C# SDK" Style="{StaticResource PhoneTextNormalStyle}"/>
+ <TextBlock x:Name="PageTitle" Text="facebook" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
- <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"></Grid>
+ <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
+ <Grid>
+ <Grid.RowDefinitions>
+ <RowDefinition Height="Auto"/>
+ <RowDefinition Height="Auto"/>
+ <RowDefinition Height="Auto"/>
+ </Grid.RowDefinitions>
+ <Grid.ColumnDefinitions>
+ <ColumnDefinition Width="Auto"/>
+ <ColumnDefinition Width="*"/>
+ <ColumnDefinition Width="*"/>
+ </Grid.ColumnDefinitions>
+ <Image x:Name="picProfile" Width="100" Height="100" HorizontalAlignment="Left" />
+ <StackPanel Grid.Column="1" Margin="8,0,0,0">
+ <TextBlock x:Name="ProfileName"></TextBlock>
+ <TextBlock x:Name="TotalFriends"></TextBlock>
+ <TextBlock x:Name="FirstName"></TextBlock>
+ <TextBlock x:Name="LastName"></TextBlock>
+ </StackPanel>
+ <StackPanel Grid.Row="1" Grid.ColumnSpan="3" Margin="0,5,0,0">
+ <TextBox Height="150" x:Name="txtMessage" AcceptsReturn="True" VerticalScrollBarVisibility="Auto" TextWrapping="Wrap"></TextBox>
+ <StackPanel Orientation="Horizontal">
+ <Button Content="post to wall" Click="PostToWall_Click"/>
+ <Button Content="delete last message" IsEnabled="False" Click="DeleteLastMessage_Click" x:Name="btnDeleteLastMessage"/>
+ </StackPanel>
+ </StackPanel>
+ </Grid>
+ </Grid>
</Grid>
-
+
<!--Sample code showing usage of ApplicationBar-->
<!--<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
@@ -41,5 +69,5 @@
</shell:ApplicationBar.MenuItems>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>-->
-
+
</phone:PhoneApplicationPage>
@@ -10,14 +10,167 @@
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
+using System.Windows.Media.Imaging;
+using Facebook;
namespace facebook_windows_phone_sample.Pages
{
public partial class FacebookInfoPage : PhoneApplicationPage
{
+ private string _accessToken;
+ private string _userId;
+
public FacebookInfoPage()
{
InitializeComponent();
}
+
+ protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
+ {
+ base.OnNavigatedTo(e);
+ _accessToken = NavigationContext.QueryString["access_token"];
+ _userId = NavigationContext.QueryString["id"];
+ }
+
+ private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
+ {
+ LoadFacebookData();
+ }
+
+ private void LoadFacebookData()
+ {
+ GetUserProfilePicture();
+
+ GraphApiSample();
+
+ FqlSample();
+ }
+
+ private void GetUserProfilePicture()
+ {
+ // available picture types: square (50x50), small (50xvariable height), large (about 200x variable height) (all size in pixels)
+ // for more info visit http://developers.facebook.com/docs/reference/api
+ string profilePictureUrl = string.Format("https://graph.facebook.com/{0}/picture?type={1}&access_token={2}", _userId, "square", _accessToken);
+
+ picProfile.Source = new BitmapImage(new Uri(profilePictureUrl));
+ }
+
+ private void GraphApiSample()
+ {
+ var fb = new FacebookClient(_accessToken);
+
+ fb.GetCompleted += (o, e) =>
+ {
+ if (e.Error != null)
+ {
+ Dispatcher.BeginInvoke(() => MessageBox.Show(e.Error.Message));
+ return;
+ }
+
+ var result = (IDictionary<string, object>)e.GetResultData();
+
+ Dispatcher.BeginInvoke(() =>
+ {
+ ProfileName.Text = "Hi " + (string)result["name"];
+ FirstName.Text = "First Name: " + (string)result["first_name"];
+ FirstName.Text = "Last Name: " + (string)result["last_name"];
+ });
+ };
+
+ fb.GetAsync("me");
+ }
+
+ private void FqlSample()
+ {
+ var fb = new FacebookClient(_accessToken);
+
+ fb.GetCompleted += (o, e) =>
+ {
+ if (e.Error != null)
+ {
+ Dispatcher.BeginInvoke(() => MessageBox.Show(e.Error.Message));
+ return;
+ }
+
+ var result = (IDictionary<string, object>)e.GetResultData();
+ var data = (IList<object>)result["data"];
+
+ var count = data.Count;
+
+ // since this is an async callback, make sure to be on the right thread
+ // when working with the UI.
+ Dispatcher.BeginInvoke(() =>
+ {
+ TotalFriends.Text = string.Format("You have {0} friend(s).", count);
+ });
+ };
+
+ // query to get all the friends
+ var query = string.Format("SELECT uid,pic_square FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1={0})", "me()");
+
+ // Note: For windows phone 7, make sure to add [assembly: InternalsVisibleTo("Facebook")] if you are using anonymous objects as parameter.
+ fb.GetAsync("fql", new { q = query });
+ }
+
+ private string _lastMessageId;
+ private void PostToWall_Click(object sender, RoutedEventArgs e)
+ {
+ if (string.IsNullOrEmpty(txtMessage.Text))
+ {
+ MessageBox.Show("Enter message.");
+ return;
+ }
+
+ var fb = new FacebookClient(_accessToken);
+
+ fb.PostCompleted += (o, args) =>
+ {
+ if (args.Error != null)
+ {
+ Dispatcher.BeginInvoke(() => MessageBox.Show(args.Error.Message));
+ return;
+ }
+
+ var result = (IDictionary<string, object>)args.GetResultData();
+ _lastMessageId = (string)result["id"];
+
+ Dispatcher.BeginInvoke(() =>
+ {
+ MessageBox.Show("Message Posted successfully");
+
+ txtMessage.Text = string.Empty;
+ btnDeleteLastMessage.IsEnabled = true;
+ });
+ };
+
+ var parameters = new Dictionary<string, object>();
+ parameters["message"] = txtMessage.Text;
+
+ fb.PostAsync("me/feed", parameters);
+ }
+
+ private void DeleteLastMessage_Click(object sender, RoutedEventArgs e)
+ {
+ btnDeleteLastMessage.IsEnabled = false;
+
+ var fb = new FacebookClient(_accessToken);
+
+ fb.DeleteCompleted += (o, args) =>
+ {
+ if (args.Error != null)
+ {
+ Dispatcher.BeginInvoke(() => MessageBox.Show(args.Error.Message));
+ return;
+ }
+
+ Dispatcher.BeginInvoke(() =>
+ {
+ MessageBox.Show("Message deleted successfully");
+ btnDeleteLastMessage.IsEnabled = false;
+ });
+ };
+
+ fb.DeleteAsync(_lastMessageId);
+ }
}
}
@@ -86,7 +86,7 @@ private void LoginSucceded(string accessToken)
{
if (e.Error != null)
{
- MessageBox.Show(e.Error.Message);
+ Dispatcher.BeginInvoke(() => MessageBox.Show(e.Error.Message));
return;
}
@@ -35,3 +35,5 @@
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: NeutralResourcesLanguageAttribute("en-US")]
+
+[assembly: InternalsVisibleTo("Facebook")]

0 comments on commit 9824720

Please sign in to comment.