简体   繁体   中英

WP7 Skydrive API - creating folder doesnt work

I try this tutorial to create new folder on skydrive from my WP7 app.

Here is my code:

    private void MSAccountLoginToggleSwitch_Checked_1(object sender, RoutedEventArgs e)
    {
        try
        {
            LiveAuthClient auth = new LiveAuthClient("** my id **");
            auth.LoginAsync(new string[] { "wl.skydrive_update", "wl.calendars_update" });
            auth.LoginCompleted += auth_LoginCompleted;
        }
        catch (LiveAuthException exception)
        {
            MessageBox.Show("Error signing in: " + exception.Message);
        }
    }

    private void auth_LoginCompleted(object sender, LoginCompletedEventArgs e)
    {
        if (e.Status == LiveConnectSessionStatus.Connected)
        {
            mySession = e.Session;
        }
        else
        {
            MSAccountLoginToggleSwitch.IsChecked = false;
        }
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        try
        {
            var folderData = new Dictionary<string, object>();
            folderData.Add("some test", "A brand new folder was created");

            LiveConnectClient liveClient = new LiveConnectClient(mySession);
            liveClient.PostAsync("me/skydrive", folderData);
        }
        catch (LiveConnectException exception)
        {
            MessageBox.Show("Error creating folder: " + exception.Message);
        }
        finally
        {
            MessageBox.Show("uploded");
        }
    }

it show me messagebox "uploaded", but when I look on my skydrive that file was not created.

It doesnt show any error message, what Im doing worng?

This line liveClient.PostAsync("me/skydrive", folderData); gives you a Task which you do not wait, you just show MessageBox.Show("uploded"); at the end. I don't think that async / await are supported in WP7, so you will need to handle Task with ContinueWith method:

private void Button_Click_1(object sender, RoutedEventArgs e)
{
        var folderData = new Dictionary<string, object>();
        folderData.Add("some test", "A brand new folder was created");

        LiveConnectClient liveClient = new LiveConnectClient(mySession);
        liveClient.PostAsync("me/skydrive", folderData)
                  .ContinueWith((t) => 
                                 { 
                                    if (t.IsFauled)
                                    {
                                       MessageBox.Show("Error creating folder: " + t.Exception.Message);
                                    }
                                    else
                                    {
                                        MessageBox.Show("uploded");  
                                    }
                                 }
                                , TaskScheduler.FromCurrentSynchronizationContext());

}

UPDATED: Code above will work only on WP8, but on WP7 PostAsync is not a method with Task, so to get PostAsync result you need to subscribe to PostCompleted event.

I found problem I have mistake in line:

folderData.Add("some test", "A brand new folder was created");

correct version is:

folderData.Add("name", "some test");
   var folderData = new Dictionary<string,object>();
   folderData.Add("myfolder ","simple folder ");    
   client.PostAsync("me/skydrive","{'name': 'myfolder' }"); 

 client.PostCompleted += new EventHandler<LiveOperationCompletedEventArgs>          (client_PostCompleted);

   void client_PostCompleted(object sender, LiveOperationCompletedEventArgs e)
    {
        var a = e.RawResult;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM