Normally I develop apps for Windows Phone and Windows Store and when I started to use some libraries related with Xamarin, I found some libraries that don´t use the async/await and for me it is fundamental.
One library that I think should use asyn/await but that I saw does not use it is Xamarin.Auth!
For uisng Xamarin.Auth.OAuth2Authenticator for authentication I recommend the following solution.
For Xamarin.Android
- public async Task<Account> LoginAsync(Activity activity, bool allowCancel)
- {
- var auth = new OAuth2Authenticator(
- clientId: "<scopes here>",
- scope: "<scopes here>",
- authorizeUrl: new Uri("<url here>"),
- redirectUrl: new Uri("<url here>"))
- {
- AllowCancel = allowCancel
- };
- // If authorization succeeds or is canceled, .Completed will be fired.
- var tcs1 = new TaskCompletionSource<AuthenticatorCompletedEventArgs>();
- EventHandler<AuthenticatorCompletedEventArgs> d1 =
- (o, e) =>
- {
- try
- {
- tcs1.TrySetResult(e);
- }
- catch (Exception ex)
- {
- tcs1.TrySetResult(new AuthenticatorCompletedEventArgs(null));
- }
- };
- try
- {
- auth.Completed += d1;
- var intent = auth.GetUI(activity);
- activity.StartActivity(intent);
- var result= await tcs1.Task;
- return result.Account;
- }
- catch (Exception)
- {
- // todo you should handle the exception
- return null;
- }
- finally
- {
- auth.Completed -= d1;
- }
- }
- var authService = new AuthService();
- var result = await authService.LoginAsync(this, allowCancel);
- public async Task<Account> LoginAsync(DialogViewController dialog, bool allowCancel)
- {
- var auth = new OAuth2Authenticator(
- clientId: "<scopes here>",
- scope: "<scopes here>",
- authorizeUrl: new Uri("<url here>"),
- redirectUrl: new Uri("<url here>"))
- {
- AllowCancel = allowCancel
- };
- // If authorization succeeds or is canceled, .Completed will be fired.
- var tcs1 = new TaskCompletionSource<AuthenticatorCompletedEventArgs>();
- EventHandler<AuthenticatorCompletedEventArgs> d1 =
- (o, e) =>
- {
- try
- {
- tcs1.TrySetResult(e);
- }
- catch (Exception ex)
- {
- tcs1.TrySetResult(new AuthenticatorCompletedEventArgs(null));
- }
- };
- try
- {
- auth.Completed += d1;
- var vc = auth.GetUI();
- dialog.PresentViewController(vc, true, null);
- var result= await tcs1.Task;
- return result.Account;
- }
- catch (Exception)
- {
- // todo handle the exception
- return null;
- }
- finally
- {
- auth.Completed -= d1;
- }
- }
- var authService = new AuthService();
- var result = await authService.LoginAsync(dialog, allowCancel);
I added the sample to: https://github.com/saramgsilva/Xamarin.Auth/tree/master/samples and will do a pull request for the original repository.

Sara SilvaPosted Jun 9, 2014, 4:41 AM
Xamarin 3 bring new features to Xamarin Studio and Xamarin for Visual Studio, and they published new libs that allow to create a common user interface that is set to native controls when the UI is loaded. For example with the knowlege i have from WP, now i can create Android and IOS apps, is fantastic! I hope to write more about...
Mahesh ChandPosted Jun 8, 2014, 11:31 PM
Sara, how do you like Xamarin 3? I have not played with it yet but am curious to know.