[英]WP7 Skydrive API - creating folder doesnt work
我尝试通过本教程在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上查看时,未创建该文件。
它没有显示任何错误消息,我在做什么?
这行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事件。
我发现了我在行中出错的问题:
folderData.Add("some test", "A brand new folder was created");
正确的版本是:
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.