【问题标题】:Await in Windows Phone 7?在 Windows Phone 7 中等待?
【发布时间】:2014-02-11 19:04:26
【问题描述】:

我正在尝试查看Windows Phone Live tutorial,但在尝试实现代码示例时遇到了困难,因为它似乎缺少信息。

using System;
using System.Windows;
using System.Collections.Generic;
using Microsoft.Phone.Controls;
using Microsoft.Live;
using Microsoft.Live.Controls;

namespace WindowsPhoneCodeSample
{
    public partial class MainPage : PhoneApplicationPage
    {
        private LiveConnectClient client;

        public MainPage()
        {
            InitializeComponent();            
        }

        private async void btnSignin_SessionChanged(object sender, LiveConnectSessionChangedEventArgs e)
        {
            if (e.Status == LiveConnectSessionStatus.Connected)
            {
                client = new LiveConnectClient(e.Session);
                LiveOperationResult operationResult = await client.GetAsync("me");
                try
                { 
                    dynamic meResult = operationResult.Result;
                    if (meResult.first_name != null &&
                        meResult.last_name != null)
                    {
                        infoTextBlock.Text = "Hello " +
                            meResult.first_name + " " +
                            meResult.last_name + "!";
                    }
                    else
                    {
                        infoTextBlock.Text = "Hello, signed-in user!";
                    }
                }
                catch (LiveConnectException exception)
                {
                    this.infoTextBlock.Text = "Error calling API: " +
                        exception.Message;
                }
            }
            else
            {
                infoTextBlock.Text = "Not signed in.";
            }
        }
    }
}

我明白了

Error   2   The name 'await' does not exist in the current context  

Error   3   One or more types required to compile a dynamic expression cannot be found. Are you missing references to Microsoft.CSharp.dll and System.Core.dll? 

我是否遗漏了一些参考资料或其他什么?

编辑

本教程似乎做得不好或非常过时。我制作了一个 windows phone 8 应用程序,但由于“await”t 关键字,它仍然无法构建。

The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.


client.GetAsync is also a void method. So not sure how it returns something as well.

【问题讨论】:

  • await 和 async 是 .NET 4.5 的特性。 Windows Phone 7 是否使用 .NET 4.5?
  • 嗯,我想本教程会说我需要一个 3rd 方包。
  • 您使用的是哪个版本的 Visual Studio?
  • @chobo2:它不是第 3 方——它来自微软。我仍然很惊讶它没有提到它。顺便说一句,您是在为 Windows Phone 7.5 开发吗?

标签: c# windows-phone-7 async-await live


【解决方案1】:

await 仅适用于 awaitables - 请参阅 C# Language Specification 上的 §7.7.7.1。

由于client.GetAsync 是一个 void 返回方法,它永远不可能是一个可等待的

【讨论】:

  • 是的,这就是我得出的结论,这也是我对本教程感到非常困惑的原因。
  • 根据GetAsync method的文档返回Task<LiveOperationResult>。您使用的是哪个版本的 Live SDK?
  • 我使用的是通过教程中的链接获得的 5.5(microsoft.com/en-us/download/details.aspx?id=40739) 版本
  • 我提供的链接上的文档的日期约为 3 个月。您链接中的示例的日期约为 2 年,您可以see for your self。但是,您链接上的下载似乎已有 4 个月的历史。 (...)
  • (...) 从该代码中,您可以学到两件事:1)糟糕的命名会引起用户的注意; 2)如何在EAP(基于事件的异步模式)之上实现TPA(基于任务的异步模式)。该类中的 Task-asynchronous 方法应该以 TaskAsync 为后缀,因为已经存在以 async 为后缀的非 Task-asynchronous 方法。
【解决方案2】:

您需要安装 NuGet 包 Microsoft.Bcl.Async(您可以在 NuGet 管理器中找到它)以便编译器识别单词 await 和 async。 更多信息http://www.nuget.org/packages/Microsoft.Bcl.Async/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多