Пример #1
0
        internal Platform()
        {
            _renderer = new PlatformRenderer(this);
            _modals   = new List <Page>();

            SubscribeToAlertsAndActionSheets();
        }
		internal Platform()
		{
			_renderer = new PlatformRenderer(this);
			_modals = new List<Page>();

			var busyCount = 0;
			MessagingCenter.Subscribe(this, Page.BusySetSignalName, (Page sender, bool enabled) =>
			{
				if (!PageIsChildOfPlatform(sender))
					return;
				busyCount = Math.Max(0, enabled ? busyCount + 1 : busyCount - 1);
				UIApplication.SharedApplication.NetworkActivityIndicatorVisible = busyCount > 0;
			});

			MessagingCenter.Subscribe(this, Page.AlertSignalName, (Page sender, AlertArguments arguments) =>
			{
				if (!PageIsChildOfPlatform(sender))
					return;
				PresentAlert(arguments);
			});

			MessagingCenter.Subscribe(this, Page.ActionSheetSignalName, (Page sender, ActionSheetArguments arguments) =>
			{
				if (!PageIsChildOfPlatform(sender))
					return;

				var pageRoot = sender;
				while (!Application.IsApplicationOrNull(pageRoot.RealParent))
					pageRoot = (Page)pageRoot.RealParent;

				PresentActionSheet(arguments);
			});
		}
Пример #3
0
        internal Platform()
        {
            _renderer = new PlatformRenderer(this);
            _modals   = new List <Page>();

            var busyCount = 0;

            MessagingCenter.Subscribe(this, Page.BusySetSignalName, (Page sender, bool enabled) =>
            {
                if (!PageIsChildOfPlatform(sender))
                {
                    return;
                }
                busyCount = Math.Max(0, enabled ? busyCount + 1 : busyCount - 1);
                UIApplication.SharedApplication.NetworkActivityIndicatorVisible = busyCount > 0;
            });

            MessagingCenter.Subscribe(this, Page.AlertSignalName, (Page sender, AlertArguments arguments) =>
            {
                if (!PageIsChildOfPlatform(sender))
                {
                    return;
                }

                if (Forms.IsiOS8OrNewer)
                {
                    PresentAlert(arguments);
                }
                else
                {
                    UIAlertView alertView;
                    if (arguments.Accept != null)
                    {
                        alertView = new UIAlertView(arguments.Title, arguments.Message, null, arguments.Cancel, arguments.Accept);
                    }
                    else
                    {
                        alertView = new UIAlertView(arguments.Title, arguments.Message, null, arguments.Cancel);
                    }

                    alertView.Dismissed += (o, args) => arguments.SetResult(args.ButtonIndex != 0);
                    alertView.Show();
                }
            });

            MessagingCenter.Subscribe(this, Page.ActionSheetSignalName, (Page sender, ActionSheetArguments arguments) =>
            {
                if (!PageIsChildOfPlatform(sender))
                {
                    return;
                }

                var pageRoot = sender;
                while (!Application.IsApplicationOrNull(pageRoot.RealParent))
                {
                    pageRoot = (Page)pageRoot.RealParent;
                }
                var pageRenderer = GetRenderer(pageRoot);

                if (Forms.IsiOS8OrNewer)
                {
                    PresentAlert(arguments);
                }
                else
                {
                    var actionSheet = new UIActionSheet(arguments.Title, null, arguments.Cancel, arguments.Destruction, arguments.Buttons.ToArray());

                    actionSheet.ShowInView(pageRenderer.NativeView);

                    actionSheet.Clicked += (o, args) =>
                    {
                        string title = null;
                        if (args.ButtonIndex != -1)
                        {
                            title = actionSheet.ButtonTitle(args.ButtonIndex);
                        }

                        // iOS 8 always calls WillDismiss twice, once with the real result, and again with -1.
                        arguments.Result.TrySetResult(title);
                    };
                }
            });
        }
Пример #4
0
        internal Platform()
        {
            _renderer = new PlatformRenderer(this);
            _modals   = new List <Page>();

            var busyCount = 0;

            MessagingCenter.Subscribe(this, Page.BusySetSignalName, (Page sender, bool enabled) =>
            {
                if (!PageIsChildOfPlatform(sender))
                {
                    return;
                }
                busyCount = Math.Max(0, enabled ? busyCount + 1 : busyCount - 1);
                UIApplication.SharedApplication.NetworkActivityIndicatorVisible = busyCount > 0;
            });

            MessagingCenter.Subscribe(this, Page.AlertSignalName, (Page sender, AlertArguments arguments) =>
            {
                if (!PageIsChildOfPlatform(sender))
                {
                    return;
                }

                if (Forms.IsiOS8OrNewer)
                {
                    var alert        = UIAlertController.Create(arguments.Title, arguments.Message, UIAlertControllerStyle.Alert);
                    var oldFrame     = alert.View.Frame;
                    alert.View.Frame = new RectangleF(oldFrame.X, oldFrame.Y, oldFrame.Width, oldFrame.Height - _alertPadding * 2);
                    alert.AddAction(UIAlertAction.Create(arguments.Cancel, UIAlertActionStyle.Cancel, a => arguments.SetResult(false)));
                    if (arguments.Accept != null)
                    {
                        alert.AddAction(UIAlertAction.Create(arguments.Accept, UIAlertActionStyle.Default, a => arguments.SetResult(true)));
                    }
                    var page = _modals.Any() ? _modals.Last() : Page;
                    var vc   = GetRenderer(page).ViewController;
                    vc.PresentViewController(alert, true, null);
                }
                else
                {
                    UIAlertView alertView;
                    if (arguments.Accept != null)
                    {
                        alertView = new UIAlertView(arguments.Title, arguments.Message, null, arguments.Cancel, arguments.Accept);
                    }
                    else
                    {
                        alertView = new UIAlertView(arguments.Title, arguments.Message, null, arguments.Cancel);
                    }

                    alertView.Dismissed += (o, args) => arguments.SetResult(args.ButtonIndex != 0);
                    alertView.Show();
                }
            });

            MessagingCenter.Subscribe(this, Page.ActionSheetSignalName, (Page sender, ActionSheetArguments arguments) =>
            {
                if (!PageIsChildOfPlatform(sender))
                {
                    return;
                }

                var pageRoot = sender;
                while (!Application.IsApplicationOrNull(pageRoot.RealParent))
                {
                    pageRoot = (Page)pageRoot.RealParent;
                }
                var pageRenderer = GetRenderer(pageRoot);

                if (Forms.IsiOS8OrNewer)
                {
                    var alert = UIAlertController.Create(arguments.Title, null, UIAlertControllerStyle.ActionSheet);

                    if (arguments.Cancel != null)
                    {
                        alert.AddAction(UIAlertAction.Create(arguments.Cancel, UIAlertActionStyle.Cancel, a => arguments.SetResult(arguments.Cancel)));
                    }

                    if (arguments.Destruction != null)
                    {
                        alert.AddAction(UIAlertAction.Create(arguments.Destruction, UIAlertActionStyle.Destructive, a => arguments.SetResult(arguments.Destruction)));
                    }

                    foreach (var label in arguments.Buttons)
                    {
                        if (label == null)
                        {
                            continue;
                        }

                        var blabel = label;
                        alert.AddAction(UIAlertAction.Create(blabel, UIAlertActionStyle.Default, a => arguments.SetResult(blabel)));
                    }

                    if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
                    {
                        UIDevice.CurrentDevice.BeginGeneratingDeviceOrientationNotifications();
                        var observer = NSNotificationCenter.DefaultCenter.AddObserver(UIDevice.OrientationDidChangeNotification,
                                                                                      n => { alert.PopoverPresentationController.SourceRect = pageRenderer.ViewController.View.Bounds; });

                        arguments.Result.Task.ContinueWith(t =>
                        {
                            NSNotificationCenter.DefaultCenter.RemoveObserver(observer);
                            UIDevice.CurrentDevice.EndGeneratingDeviceOrientationNotifications();
                        }, TaskScheduler.FromCurrentSynchronizationContext());

                        alert.PopoverPresentationController.SourceView = pageRenderer.ViewController.View;
                        alert.PopoverPresentationController.SourceRect = pageRenderer.ViewController.View.Bounds;
                        alert.PopoverPresentationController.PermittedArrowDirections = 0;                         // No arrow
                    }

                    pageRenderer.ViewController.PresentViewController(alert, true, null);
                }
                else
                {
                    var actionSheet = new UIActionSheet(arguments.Title, null, arguments.Cancel, arguments.Destruction, arguments.Buttons.ToArray());

                    actionSheet.ShowInView(pageRenderer.NativeView);

                    actionSheet.Clicked += (o, args) =>
                    {
                        string title = null;
                        if (args.ButtonIndex != -1)
                        {
                            title = actionSheet.ButtonTitle(args.ButtonIndex);
                        }

                        // iOS 8 always calls WillDismiss twice, once with the real result, and again with -1.
                        arguments.Result.TrySetResult(title);
                    };
                }
            });
        }