Using Skydrive in your Windows Phone applications: part2

This is part two on how to use Skydrive in your Windows Phone applications.  Part one was an introduction to how to sign in into Skydrive.

In this part, let’s try to read the Skydrive’s folders.

I will use the ApplicationBarIconButton to read from my Skydrive then put the result in a very simple list in another page.

So first, let’s create a new Windows Phone page , I’ll call it “ content.xaml”.

I’ll change the Application title to “ Skydrive” and the page title to “ Folders”. I will also add a listbox.

 1:
 2:         <!--TitlePanel contains the name of the application and page title-->
 3:         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
 4:             <TextBlock x:Name="ApplicationTitle" Text="Skydrive" Style="{StaticResource PhoneTextNormalStyle}"/>
 5:             <TextBlock x:Name="PageTitle" Text="Folders" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
 6:         </StackPanel>
 7:
 8:         <!--ContentPanel - place additional content here-->
 9:         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
 10:             <ListBox Name="list">
 11:                 <ListBox.ItemTemplate>
 12:                     <DataTemplate>
 13:                         <StackPanel Orientation="Horizontal">
 14:                             <TextBlock Name="folder" Text="{Binding Name}"/>
 15:                         </StackPanel>
 16:                     </DataTemplate>
 17:                 </ListBox.ItemTemplate>
 18:
 19:             </ListBox>
 20:         </Grid>

Now we need to fill that listBox.

In the MainPage.xaml, let’s add an ApplicationBarIconButton ( it is already there as a commented code):

 1: <phone:PhoneApplicationPage.ApplicationBar>
 2:        <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
 3:            <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Read " Click="ApplicationBarIconButton_Click" />
 4:        </shell:ApplicationBar>
 5:    </phone:PhoneApplicationPage.ApplicationBar>
 6: /phone:PhoneApplicationPage>

When you click on the button , you should be able to get your content from Skydrive if you are signed in and see it on that listbox we put in “content.xaml”

 1: private void ApplicationBarIconButton_Click(object sender, EventArgs e)
 2:        {
 3:            if (session == null)
 4:            {
 5:                infoTextBlock.Text = "You must sign in first.";
 6:            }
 7:            else
 8:            {
 9:                LiveConnectClient client = new LiveConnectClient(session);
 10:                client.GetCompleted +=
 11:                    new EventHandler<LiveOperationCompletedEventArgs>(getFolderProperties_Completed);
 12:                client.GetAsync("/me/skydrive/files");
 13:            }
 14:
 15:        }
 16:
 17:        void getFolderProperties_Completed(object sender, LiveOperationCompletedEventArgs e)
 18:        {
 19:            if (e.Error == null)
 20:            {
 21:                List<object> data = (List<object>)e.Result["data"];
 22:                foreach (IDictionary<string, object> content in data)
 23:                {
 24:                    SkyDriveContent skyContent = new SkyDriveContent();
 25:                    skyContent.Name = (string)content["name"];
 26:                    ContentList.Add(skyContent);
 27:                }
 28:                this.NavigationService.Navigate(new Uri("/content.xaml", UriKind.Relative));
 29:            }
 30:            else
 31:            {
 32:                infoTextBlock.Text = "Error calling API: " + e.Error.Message;
 33:            }
 34:
 35:        }

The result is stored in a “ SkydriveContent “ list :

 1: public static List<SkyDriveContent> ContentList = new List<SkyDriveContent>();

 

 1: public class SkyDriveContent
 2:    {   public string Name { get; set; }
 3:        public string Description { get; set; }
 4:    }

In the Content.xaml.cs , we will tell the listbox to get its items from the ContentList:

 1: public content()
 2:        {
 3:            InitializeComponent();
 4:            list.ItemsSource = MainPage.ContentList;
 5:        }

That is all you need to be able to read your Skydrive folders in your Windows Phone application.

There is also an interesting and helpful post in Silverlight Show by Samidip Basu about the usage of Skydrive with Windows Phone.

About these ads

About rabeb

Rabeb is a software engineer , fan of the latest .Net technologies, playing with Metro style applications for Win8 at work . In her spare time,she plays with Metro Style apps for Windows Phone.

Posted on February 2, 2012, in Mango, WP7 and tagged , . Bookmark the permalink. 2 Comments.

  1. Any chance to post a working sample on how to upload/download XML files to/from WP ?

    thanks!

  1. Pingback: Dew Drop – February 3, 2012 (#1,258) | Alvin Ashcraft's Morning Dew

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

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: