2014-05-12 97 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方法可以終止您的應用程序,並且當您的應用程序恢復時,它可以完成任務,但不會有任何等待它的方法。

您可能會有一個「任務緩存」在暫停/恢復時被序列化,並且您的方法從該緩存中提取,只有在找不到任務時才調用其他方法。我會說這種方法充滿了陷阱,但是完全不值得。