对于Windows Phone 8.1,Microsoft引入了一些方法,以AndContinue结尾。这些方法会挂起应用程序以执行和处理用户输入。之后,他们使用包含操作结果的对象调用Continue...方法。

一个示例是WebAuthenticationBroker.AuthenticateAndContinue,用于OAuth。

示例代码:

class Test : IWebAuthenticationContinuable
{
    private void StartAuth()
    {
        WebAuthenticationBroker.AuthenticateAndContinue(new Uri("http://example.org/token?someInformations"),
                new Uri("http://example.org/callback"), null, WebAuthenticationOptions.None);
    }

    private void ContinueWebAuthentication(WebAuthenticationBrokerContinuationEventArgs args)
    {
        WebAuthenticationResult result = args.WebAuthenticationResult;

        //Do something with the result
    }
}


在Windows应用商店中,使用WebAuthenticationBroker.AuthenticateAsync -emthod可以实现相同的目的。此方法是一个简单的异步操作。

我想使用AuthenticateAsync为Windows Phone编写AuthenticateAndContinue方法。它必须返回Task<WebAuthenticationResult>

作为一种非常棘手的方法,我考虑了Task,该ContinueWebAuthentication是在执行之后完成的。如果我等待此任务并将结果设置为某个变量,则可以在async方法中访问它并返回它。

但是我无法弄清楚如何实现这一点。

最佳答案

AndContinue API不是像Windows Store AuthenticateAsync那样的异步调用。 From MSDN


  当您调用AndContinue方法时,在操作完成时将停用您的应用程序,以节省内存。在低内存设备上,您的应用甚至可能被终止。


因此,当您调用AndContinue方法时,您的应用程序可以终止。还原应用程序后,您需要某种方式跳回到您的async方法。没有什么比这内置的AFAIK更好。

当然可以使用Task<WebAuthenticationResult>创建TaskCompletionSource<T>,但是在这种情况下将无法使用。 AndContinue方法可以终止您的应用程序,当您的应用程序恢复运行时,它可以完成任务,但是不会等待任何async方法。

您可能有一个在挂起/恢复时序列化的“任务缓存”,并让您的async方法从该缓存中拉出,仅在找不到任务时才调用其他async方法。我会说这种方法充满陷阱,而且完全不值得。

关于c# - andcontinue()方法作为异步操作执行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23613832/

10-14 16:36
查看更多