private void LoginCanceled(object sender, EventArgs e)
        {
            // Remove the login UI.
            _loginUI.Hide();
            _loginUI = null;

            // Cancel the task completion source task.
            _loginTaskCompletionSource.TrySetCanceled();
        }
        private void ShowLoginUI()
        {
            // Get the URL for the service being requested.
            var info       = _loginTaskCompletionSource.Task.AsyncState as CredentialRequestInfo;
            var serviceUrl = info.ServiceUri.GetLeftPart(UriPartial.Path);

            // Create a view to show login controls over the map view.
            var ovBounds = new CoreGraphics.CGRect(0, 80, _myMapView.Bounds.Width, _myMapView.Bounds.Height - 80);

            _loginUI = new LoginOverlay(ovBounds, 0.85f, UIColor.DarkGray, serviceUrl);

            // Handle the login event to get the login entered by the user.
            _loginUI.OnLoginInfoEntered += LoginEntered;

            // Handle the cancel event when the user closes the dialog without entering a login.
            _loginUI.OnCanceled += LoginCanceled;

            // Add the login UI view (will display semi-transparent over the map view)
            View.Add(_loginUI);
        }
        // Handle the OnLoginEntered event from the login UI.
        // LoginEventArgs contains the username and password that were entered.
        private async void LoginEntered(object sender, LoginEventArgs e)
        {
            // Make sure the task completion source has all the information needed.
            if (_loginTaskCompletionSource == null ||
                _loginTaskCompletionSource.Task == null ||
                _loginTaskCompletionSource.Task.AsyncState == null)
            {
                return;
            }

            try
            {
                // Get the associated CredentialRequestInfo (will need the URI of the service being accessed).
                CredentialRequestInfo requestInfo = _loginTaskCompletionSource.Task.AsyncState as CredentialRequestInfo;

                // Create a token credential using the provided username and password.
                TokenCredential userCredentials = await AuthenticationManager.Current.GenerateCredentialAsync
                                                      (requestInfo.ServiceUri,
                                                      e.Username,
                                                      e.Password,
                                                      requestInfo.GenerateTokenOptions);

                // Set the result on the task completion source.
                _loginTaskCompletionSource.TrySetResult(userCredentials);
            }
            catch (Exception ex)
            {
                // Unable to create credential, set the exception on the task completion source.
                _loginTaskCompletionSource.TrySetException(ex);
            }
            finally
            {
                // Get rid of the login controls.
                _loginUI.Hide();
                _loginUI = null;
            }
        }