1

我尝试使用教程从我的 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 时,该文件未创建。

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

4

3 回答 3

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事件。

于 2013-04-08T22:45:37.243 回答
1

我发现问题我有错误:

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

正确的版本是:

folderData.Add("name", "some test");
于 2013-04-09T09:57:58.630 回答
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;
    }
于 2013-10-01T11:06:24.450 回答