WP7 Skydrive API -创建文件夹不工作
本文关键字:文件夹 工作 创建 Skydrive API WP7 | 更新日期: 2023-09-27 18:08:03
我试着从我的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方法处理任务:
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不是任务的方法,所以要获得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;
}