Metro, skydrive, Windows 8, WinRT

skydrive upload c# metro

Peter Daukintis

A preview of the latest Live SDK for Metro apps can be downloaded here https://connect.microsoft.com/site1226. This is for usage with the Windows 8 Consumer Preview (Note that the SDK is a preview version with no go-live license).

After installing you can add a reference via ‘add Reference’ > and select Windows > Extensions in the Reference Manager dialog.

livesdkRef

In order to configure sign in on Windows Phone a client ID was required; this is different for Metro-style Apps as you now need to register your app package name and publisher identity here http://go.microsoft.com/fwlink/?LinkId=227628.

Once registered you will be provided with a new Package Name – copy this back into your app manifest.

Once this is complete, using the following code you will be able to sign in:

XAML:

add this namespace declaration

xmlns:live="using:Microsoft.Live.Controls"

and a sign in button

<live:SignInButton x:Name="btnSignin" Scopes="wl.signin wl.basic wl.skydrive wl.skydrive_update"
                   SessionChanged="SignInButtonSessionChanged" />

AccessRequest

 

The Live id session status is reported by the SessionChanged event so I handle it like this, storing the session object for later use:

private void SignInButtonSessionChanged(object sender, LiveConnectSessionChangedEventArgs e)
{
    if (e.Status == LiveConnectSessionStatus.Connected)
    {
        _liveConnectSession = e.Session;
        UpdateEnabled();
    }
}

Next we need a way to get hold of an image so I just used the File Picker to retrieve one (I added a way to capture an image using the camera but I couldn’t test it so it probably won’t work Sad smile)

Here’s the code for the file picker:

async private void ChooseFile(object sender, RoutedEventArgs e)
{
    FileOpenPicker openPicker = new FileOpenPicker();
    openPicker.ViewMode = PickerViewMode.Thumbnail;
    openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
    openPicker.FileTypeFilter.Add(".jpg");
    openPicker.FileTypeFilter.Add(".jpeg");
    openPicker.FileTypeFilter.Add(".png");
    StorageFile file = await openPicker.PickSingleFileAsync();

    if (file != null)
    {
        _filename = file.Name;
        BitmapImage bitmapImage = new BitmapImage();
        _stream = (FileRandomAccessStream)await file.OpenAsync(FileAccessMode.Read);
        bitmapImage.SetSource(_stream);
        myImage.Source = bitmapImage;
        UpdateEnabled();
    }
}

and finally, what it was all about, uploading to skydrive:

private void Upload(Stream data, string filename)
{
    var liveConnectClient = new LiveConnectClient(_liveConnectSession);

    liveConnectClient.UploadCompleted += LiveConnectClientUploadCompleted;
    VisualStateManager.GoToState(this, "Busy", true);
    liveConnectClient.UploadAsync("me/skydrive", filename, true, data, data);
}

Here’s the ui whilst uploading an image:

uploadspam

 

and when completed – displaying the json result returned by the upload.

 

uploadresults

 

and finally, the image on skydrive.

 

spamonskydrive

The project can be found here https://skydrive.live.com/redir.aspx?cid=4f1b7368284539e5&resid=4F1B7368284539E5!441&parid=4F1B7368284539E5!123

Tagged , , , ,

1 thought on “skydrive upload c# metro

  1. Thank you for the steps in simple language. I am not sure which one to use Azure Blob storage or Skydrive. Skydrive is free but I never used on my applications. Do you have any comments.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.