简体   繁体   English

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

[英]WP7 Skydrive API - creating folder doesnt work

I try this tutorial to create new folder on skydrive from my WP7 app. 我尝试通过教程在WP7应用中的skydrive上创建新文件夹。

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. 它向我显示消息框“已上传”,但是当我在skydrive上查看时,未创建该文件。

It doesnt show any error message, what Im doing worng? 它没有显示任何错误消息,我在做什么?

This line liveClient.PostAsync("me/skydrive", folderData); 这行liveClient.PostAsync("me/skydrive", folderData); gives you a Task which you do not wait, you just show MessageBox.Show("uploded"); 给您一个您不等待的任务,只需显示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: 我认为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());

}

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. 更新:上面的代码仅在WP8上有效,但在WP7上PostAsync不是Task的方法,因此要获取PostAsync结果,您需要预订PostCompleted事件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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