2014-05-12 53 views
1

对于windows phone 8.1 microsoft引入了一些方法,以AndContinue结尾。这些方法暂停应用程序以执行和处理用户输入。之后,他们调用一个Continue...-方法,其中包含操作结果的对象。andcontinue() - 作为异步操作执行的方法

一个示例是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 achived。这个方法是一个简单的异步操作。

我想写一个AuthenticateAsync-方法为windows手机使用AuthenticateAndContinue。它必须返回一个Task<WebAuthenticationResult>

作为一个相当古怪的方法,我想到了一个Task,这是在执行ContinueWebAuthentication后完成的。如果我等待此任务并将结果设置为某个变量,则可以使用异步方法访问它并返回它。

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

回答

4

AndContinue API不是异步调用,例如Windows Store AuthenticateAsyncFrom MSDN

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

因此,当您调用AndContinue方法时,您的应用程序可以终止。当您的应用程序恢复后,您需要一些方法跳回到您的方法中async。没有像这个内置的AFAIK。

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

您可能会有一个“任务缓存”在暂停/恢复时被序列化,并且您的方法从该缓存中提取,只有在找不到任务时才调用其他方法。我会说这种方法充满了陷阱,但是完全不值得。