WP7 Skydrive API - 创建文件夹不起作用

时间:2013-04-08 22:24:14

标签: c# silverlight windows-phone-7 xaml onedrive

我尝试this教程从我的WP7应用程序在skydrive上创建新文件夹。

这是我的代码:

    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");
        }
    }

它向我显示消息框“已上传”,但当我在我的skydrive上查看该文件未创建时。

它没有显示任何错误信息,我在做什么?

3 个答案:

答案 0 :(得分:1)

此行liveClient.PostAsync("me/skydrive", folderData);为您提供了一个您不等待的任务,您只需在结尾处显示MessageBox.Show("uploded");。我不认为WP7中支持async / await,因此您需要使用ContinueWith方法处理Task:

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());

}

更新:上面的代码仅适用于WP8,但是在WP7上,PostAsync不是使用Task的方法,因此要获得PostAsync结果,您需要订阅PostCompleted事件。

答案 1 :(得分:1)

我发现问题我错了:

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

正确的版本是:

folderData.Add("name", "some test");

答案 2 :(得分:0)

   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;
    }