DisplayAlert User Interaction with Single button

Hi Team,

I am using Display Alert in my application.
I want to navigate to another screen after user pressing the DisplayAlert ok button.

Note: I have checked with two buttons and its working. I want to acheive the same with single button .
Kindly advise.

Sample Code:
var Display = await DisplayAlert(exceptionTitle, "Saved successfully", "Ok");

Best Answer

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    Sorry, write on forum with iPhone is not so simple

  • NMackayNMackay GBInsider, University mod

    @KathirVel

    This code will show a single button dialog in iOS, You just wire it up using the dependency service, I never got roudn to
    finishing it off for iOS with regards to styling

    using System;
    using System.Threading.Tasks;
    
    namespace Foobar.Helpers
    {
        public interface IHandsetDialogService
        {
            Task<bool> ShowAlert(string message, string title, string okButton, Action callback);
            Task<bool> ShowAlertConfirm(string message, string title, string confirmButton, string cancelButton, Action<bool> callback);
        }
    }
    
    namespace Foobar.iOS.Helpers
    {
        public class DisplayAlertService : IHandsetDialogService
        {
            public async Task<bool> ShowAlert(string title, string content, string okButton, Action callback)
            {
                return await Task.Run(() => Alert(title, content, okButton, callback));
            }
    
            public async Task<bool> ShowAlertConfirm(string title, string content, string confirmButton, string cancelButton,
                Action<bool> callback)
            {
                return await Task.Run(() => AlertConfirm(title, content, confirmButton, cancelButton, callback));
            }
    
            private bool Alert(string title, string content, string okButton, Action callback)
            {
                UIApplication.SharedApplication.InvokeOnMainThread(() =>
                {
                    var alert = new UIAlertView(title, content, null, okButton);
                    if (!Equals(callback, null))
                        alert.Clicked += (sender, buttonArgs) => callback();
                    else
                        alert.Clicked += (sender, buttonArgs) => { };
                    alert.Show();
                });
    
                return true;
            }
    
            private bool AlertConfirm(string title, string content, string confirmButton, string cancelButton,
                Action<bool> callback)
            {
                UIApplication.SharedApplication.InvokeOnMainThread(() =>
                {
                    var alert = new UIAlertView(title, content, null, confirmButton, cancelButton);
                    alert.Clicked += (sender, buttonArgs) => { callback(buttonArgs.ButtonIndex == 0); };
                    alert.Show();
                });
    
                return true;
            }
        }
    }
    

    Then you could write something like this in your PCL

                    var dlg = ServiceLocator.Current.GetInstance<IDialogService>();
                    dlg.ShowMessage("Click OK to navigate","Title","OK",
                        () => { Application.Current.MainPage.Navigation.PushAsync(new LoginPage()); });
    

    I tested and it works as expected.

Sign In or Register to comment.