0

我无法编译这个简单的代码:

XAML:

xmlns:live="clr-namespace:Microsoft.Live.Controls;assembly=Microsoft.Live.Controls"

        <live:SignInButton 
            Name="SkydriveSignInButton" 
            ClientId="xxxxx" 
            Scopes="wl.skydrive_update wl.offline_access wl.basic wl.signin" 
            Branding="Skydrive" 
            TextType="SignIn" 
            SessionChanged="SkydriveSignInButton_OnSessionChanged" 
            HorizontalAlignment="Stretch" 
            VerticalAlignment="Stretch"
            Grid.Row="0"
            Grid.Column="1"
        />

C#:

    private void SkydriveSignInButton_OnSessionChanged(object sender, Microsoft.Live.Controls.LiveConnectSessionChangedEventArgs e)
    {
        if (e.Status == LiveConnectSessionStatus.Connected)
        {
            client = new LiveConnectClient(e.Session);

            ListFiles();
        }
        else
        {
            client = null;
        }
    }

    public async static void ListFiles()
    {
        LiveOperationResult operationResultGA = await client.GetAsync("me/skydrive/files");
    }

错误是:不能等待'void'

该项目在 VS2010 中,安装了 Bcl.Async 和 Live 5.4 数据包

4

2 回答 2

0

Well, the compiler is right - you cannot await void :D

You can only await Task and Task<T> types (and those with custom awaiters) but definitely not void.

The problematic method GetAsync, where does it come from? From Live SDK? I don't think that you can await those, you need to build a wrapper around it. You have GetCompleted event for that.

Also, change the return type for ListFiles method from void to Task. And change the caller code to:

await ListFiles();

Don't forget to add async to that method to. As a rule, remember to never write async void unless it is an event handler. It's async all the way down :)

于 2013-10-01T13:25:20.063 回答
0

Live SDK 包不依赖于 Bcl.Async,因此对于 WP7,它不能使用 Task(WP8 默认支持 Task,因此该版本不需要依赖项)。

您可以像这样轻松创建自己的异步任务方法:

public static class MyLveExtensionAsync
{
    public static Task<LiveOperationResult> GetResponseAsync(this LiveConnectClient client, string param)
        {
            TaskCompletionSource<LiveOperationResult> tcs=new TaskCompletionSource<LiveOperationResult>();
            EventHandler<LiveOperationCompletedEventArgs> handler = null;
            handler=(sender, arg) =>
            {
                client.GetCompleted -= handler;
                if (arg.Cancelled)
                {
                    tcs.TrySetCanceled();
                }
                else if (arg.Error != null)
                {
                    tcs.TrySetException(arg.Error);
                }
                else
                {
                    LiveOperationResult result =new LiveOperationResult(arg.Result,arg.RawResult);
                    tcs.TrySetResult(result);
                }
            };

            client.GetCompleted += handler;
             client.GetAsync(param);
            return tcs.Task;
        }

}

你可以这样使用:

LiveOperationResult operationResultGA = await client.GetResponseAsync("me/skydrive/files");
于 2013-10-01T17:26:13.127 回答