[英]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.