Forum Xamarin Xamarin.Forms

Trouble displaying UIAlert

Hello. I am trying to display an alert dialog from AppDelegate.cs, on handling a button click from a button that is declared in a PCL. The code builds without any errors, but in the simulator nothing happens when the connectButton is clicked. The simulators I have used are iPhone 6 and 7, running iOS 10.3.1. I'm fairly new to writing mobile apps in C# with Xamarin. Any help in spotting the issue is appreciated. Thanks. - Preston

        var connectButtonPtr = StartPage.connectButton;
        connectButtonPtr.Clicked += (sender, e) =>
        {
            // Define url string to access settings
            var settingsString = UIKit.UIApplication.OpenSettingsUrlString;
            var url = new NSUrl(settingsString);

            // Declare the alert
            var alert = UIAlertController.Create("Connect to Device",
                                                 "Open Bluetooth settings to connect to MicroPEM?",
                                                 UIAlertControllerStyle.Alert);

            // Configure the alert's actions
            alert.AddAction(UIAlertAction.Create("Cancel", 
                                                 UIAlertActionStyle.Cancel, 
                                                 action => {}));
            alert.AddAction(UIAlertAction.Create("Open Settings", UIAlertActionStyle.Default,
                                                 action =>
                                                 UIApplication.SharedApplication.OpenUrl(url)
                                                ));

            // Display the alert
            UIViewController theViewController = new UIViewController();
            theViewController.PresentViewController(alert,true,null);
        };            
Sign In or Register to comment.