سؤال

I'm currently building a universal app for Windows 8.1 and Windows Phone 8.1.

In Windows Runtime, the file picker has a very easy to use API that looks like this:

StorageFile file = await openPicker.PickSingleFileAsync();

It treats user input as an asynchronous operation that can be awaited. In this instance, if a user cancels, null is returned, but you could imagine an exception being thrown if the user cancels.

I was wondering if there was some way to create a page so that I can create a similar API. Specifically, I am doing OAuth 2.0 authentication with a hosted service, and I want to send the user to the authentication page hosted in a WebView and return the code for requesting access tokens if the operation is successful or throw an exception if the user does not authorize my application.

For example:

var authentication = new AuthenticationAccess();

string code = await authentication.RequestAuthorizationAsync();

The call would swap out the Page in the Frame with the Page containing the WebView's, the user would be able to authenticate and then control would return back to the calling function and the page swapped back to the original page.

I'm kind of swimming in Windows 8.1 C#/XAML documentation. From what I understand, there is a global Window object that hosts a Frame object which can have its content swapped out with the different Pages in the application. Having done some Android development, I'm familiar with the Activity model, where essentially different pages can be initiated through intents and can propagate results back up to the Activity which launched it. With this model, it seems like it would be easy to wrap this process in the model I described, but I'm a little confused of how to do the same thing in Windows. Would I need to stand up significant architecture to achieve this pattern?

Is there a simple answer to this, or am I in over my head?

هل كانت مفيدة؟

المحلول

Have you checked out the WebAuthenticationBroker yet? Perhaps that could turn out to be an easy solution to your problem.

Otherwise - you can use TaskCompletionSource to set up the authentication task that you can await, but I'd try to avoid navigating to other pages for your authentication dialog since navigation events could mess up your states and break whomever is waiting for the authentication to complete. A dialog overlay of some sort might be a better idea.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top