Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Using SkyDrive in your Windows Phone applications: Part 2

DZone's Guide to

Using SkyDrive in your Windows Phone applications: Part 2

· Mobile Zone
Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

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.

      <!--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="Skydrive" Style="{StaticResource PhoneTextNormalStyle}"/>
             <TextBlock x:Name="PageTitle" Text="Folders" 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">
             <ListBox Name="list">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                       <StackPanel Orientation="Horizontal">
                            <TextBlock Name="folder" Text="{Binding Name}"/>
                        </StackPanel>
                   </DataTemplate>
               </ListBox.ItemTemplate>

             </ListBox>
        </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):

<phone:PhoneApplicationPage.ApplicationBar>
       <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
            <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Read " Click="ApplicationBarIconButton_Click" />
        </shell:ApplicationBar>
    </phone:PhoneApplicationPage.ApplicationBar>
</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”

private void ApplicationBarIconButton_Click(object sender, EventArgs e)
{
   if (session == null)
   {
       infoTextBlock.Text = "You must sign in first.";
   }
   else
   {
       LiveConnectClient client = new LiveConnectClient(session);
        client.GetCompleted +=
            new EventHandler<LiveOperationCompletedEventArgs>(getFolderProperties_Completed);
        client.GetAsync("/me/skydrive/files");
    }

}

void getFolderProperties_Completed(object sender, LiveOperationCompletedEventArgs e)
{
    if (e.Error == null)
    {
        List<object> data = (List<object>)e.Result["data"];
        foreach (IDictionary<string, object> content in data)
        {
            SkyDriveContent skyContent = new SkyDriveContent();
            skyContent.Name = (string)content["name"];
            ContentList.Add(skyContent);
        }
        this.NavigationService.Navigate(new Uri("/content.xaml", UriKind.Relative));
    }
    else
    {
        infoTextBlock.Text = "Error calling API: " + e.Error.Message;
    }

}


The result is stored in a “ SkydriveContent “ list :

public static List<SkyDriveContent> ContentList = new List<SkyDriveContent>();
public class SkyDriveContent
    {   public string Name { get; set; }
        public string Description { get; set; }
    }


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

public content()
{
      InitializeComponent();
      list.ItemsSource = MainPage.ContentList;
}


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.


Source: http://rabeb.wordpress.com/2012/02/02/using-skydrive-in-your-windows-phone-applications-part2/

Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.

Topics:

Published at DZone with permission of Rabeb Othmani, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}