【问题标题】:Live SDK: await client.GetAsync("me/skydrive/files?filter=folders") does not returnLive SDK:等待 client.GetAsync("me/skydrive/files?filter=folders") 不返回
【发布时间】:2013-07-05 10:36:04
【问题描述】:

我正在为我的应用开发备份功能,它应该检查所需的文件夹是否已经存在,否则创建它。

由于我使用的是 VB.Net,我无法使用 GetCompleted 事件(仅在 C# 中可用,我没有经验)。

我当前在 FolderExistsOrCreate 函数中的代码是这样的:

    Private Async Function FolderExistsOrCreate(ByVal Name As String) As System.Threading.Tasks.Task(Of String)
        Dim ID As String = Nothing
        Dim firstRecheck = True
ReCheck:
        Try
            _message = "Looking for folder..."
            NotifyPropertyChanged("Message")
            NotifyPropertyChanged("SkyDrive")
            _client = New LiveConnectClient(_session)
 'it stops here and does not go further
            Dim res = Await _client.GetAsync("me/skydrive/files?filter=folders,albums")
            Dim folderData As Dictionary(Of String, Object) = DirectCast(res.Result, Dictionary(Of String, Object))
            Dim folders As List(Of Object) = DirectCast(folderData("data"), List(Of Object))

            For Each item As Object In folders
                Dim folder As Dictionary(Of String, Object) = DirectCast(item, Dictionary(Of String, Object))
                If folder("name").ToString = Name Then
                    ID = folder("id").ToString()
                    _message = "Folder exists..."
                    NotifyPropertyChanged("Message")
                    NotifyPropertyChanged("SkyDrive")
                End If
            Next

            If ID = Nothing Then
                If firstRecheck = False Then
                    _message = "Creating folder failed..."
                    NotifyPropertyChanged("Message")
                    NotifyPropertyChanged("SkyDrive")
                    Return Nothing
                End If
                _message = "Creating folder..."
                NotifyPropertyChanged("Message")
                NotifyPropertyChanged("SkyDrive")
                Dim newFolderData As New Dictionary(Of String, Object)
                newFolderData.Add("name", Name)
                _client = New LiveConnectClient(_session)
                res = Await _client.PostAsync("me/skydrive", newFolderData)
                firstRecheck = False
                GoTo ReCheck
            End If
            Return ID
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

该函数位于我构建的包含登录按钮的控件中,并且我已将我的 SkyDrive 类作为属性添加到控件中,它使用的 _session 是使用登录按钮创建的会话。

我得到了一个有效的客户端,但是所有的 GetAsync 函数都会让应用程序毫无例外地停在那里。分配的范围确实包括“skydrive_update”,因此也授予访问权限以创建文件夹,但代码甚至没有走那么远。

我在 Live SDK 论坛和 MSDN 论坛中搜索了任何类型的答案,但我只能找到使用 GetCompleted 方法的 C# 函数。

有什么想法吗?

【问题讨论】:

  • 您能解释一下为什么不能使用 VB 中的GetCompleted 吗?几乎总是如果你能用 C# 做某事,你也可以用 VB 做。
  • 它在 VB.Net 中不可用,他们在 SDK 文档中提到。文档说要这样做,但它不起作用。
  • 嗯? GetCompleted 是一个标准的 EventHandler 委托。绝对没有什么可以阻止您在 VB.NET 中订阅它。任何 .NET API 也可用于所有 .NET 语言。只有语言功能不同。
  • Live SDK 客户端在 VB.Net 中不启用 GetCompleted 事件,仅在 c# 中启用

标签: vb.net windows-phone-8 async-await live-sdk


【解决方案1】:

我猜你的调用堆栈更进一步,你调用了WaitResult,从而导致deadlock that I describe on my blog

【讨论】:

  • 现在有了.ConfigureAwait(False),我在await 操作之后就出现了异常"Unable to cast object of type 'Microsoft.Live.DynamicDictionary' to type 'System.Collections.Generic.Dictionary'2[System.String,System.Object]'。所有文档都显示了 c# 代码,但从 this blog post 和我的理解来看,它似乎是正确的。
  • 让它工作,错过了它是IDictionary而不是Dictionary
  • @IzaacJohansson:在 WP8 上,正确的解决方法是将 Wait/Result 替换为 await
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-26
  • 1970-01-01
相关资源
最近更新 更多