Exemple #1
0
        void UpdateItems()
        {
            CurrentItems = GetSwipedItems();
            var itemsLayout = new XStackLayout
            {
                Spacing       = 0,
                Orientation   = IsHorizontalSwipe ? StackOrientation.Horizontal : StackOrientation.Vertical,
                FlowDirection = SwipeDirection == SwipeDirection.Left ? FlowDirection.RightToLeft : FlowDirection.LeftToRight
            };

            foreach (var item in CurrentItems)
            {
                View itemView = null;
                if (item is SwipeItem switem)
                {
                    itemView = CreateItemView(switem, !IsHorizontalSwipe);
                }
                else if (item is SwipeItemView customItem)
                {
                    itemView = CreateItemView(customItem);
                }
                else
                {
                    continue;
                }

                var tap = new TapGestureRecognizer();
                tap.Command          = item.Command;
                tap.CommandParameter = item.CommandParameter;
                tap.Tapped          += (s, e) =>
                {
                    if (item is ISwipeItem swipeItem)
                    {
                        swipeItem.OnInvoked();
                    }

                    if (item is SwipeItemView customSwipeItem)
                    {
                        customSwipeItem.OnInvoked();
                    }

                    if (CurrentItems.SwipeBehaviorOnInvoked != SwipeBehaviorOnInvoked.RemainOpen)
                    {
                        Application.Current.Dispatcher.Dispatch(() =>
                        {
                            _ = SwipeCloseAsync();
                        });
                    }
                };
                itemView.GestureRecognizers.Add(tap);

                if (IsHorizontalSwipe)
                {
                    itemView.HorizontalOptions = LayoutOptions.Start;
                    itemView.VerticalOptions   = LayoutOptions.Fill;
                }
                else
                {
                    itemView.VerticalOptions   = LayoutOptions.Start;
                    itemView.HorizontalOptions = LayoutOptions.Fill;
                }
                itemsLayout.Children.Add(itemView);
            }

            var itemsRenderer = Platform.GetOrCreateRenderer(itemsLayout);

            (itemsRenderer as ILayoutRenderer)?.RegisterOnLayoutUpdated();
            var measured = itemsLayout.Measure(Element.Width, Element.Height);

            MaximumSwipeSize = Forms.ConvertToScaledPixel(
                IsHorizontalSwipe ?
                Math.Min(measured.Request.Width, Element.Width) :
                Math.Min(measured.Request.Height, Element.Height));

            Control.Children.Add(itemsRenderer.NativeView);

            var itemsGeometry = NativeView.Geometry;

            if (SwipeDirection == SwipeDirection.Up)
            {
                itemsGeometry.Y += (itemsGeometry.Height - MaximumSwipeSize);
            }
            itemsRenderer.NativeView.Geometry = itemsGeometry;
            itemsRenderer.NativeView.StackBelow(Platform.GetRenderer(SwipeView.Content).NativeView);

            _itemsRenderer = itemsRenderer;
        }
Exemple #2
0
        void OnPromptRequested(Page sender, PromptArguments args)
        {
            // Verify that the page making the request is child of this platform
            if (!_platform.PageIsChildOfPlatform(sender))
            {
                return;
            }

            var prompt = Native.Dialog.CreateDialog(Forms.NativeParent, (args.Accept != null));

            prompt.Title = args.Title;

            var entry = new Entry
            {
                MinimumWidthRequest = 200,
                HorizontalOptions   = LayoutOptions.Fill,
                BackgroundColor     = Color.FromRgb(250, 250, 250),
                TextColor           = Color.FromRgb(0, 0, 0),
                Keyboard            = args.Keyboard,
            };

            if (!string.IsNullOrEmpty(args.Placeholder))
            {
                entry.Placeholder = args.Placeholder;
            }
            if (args.MaxLength > 0)
            {
                entry.MaxLength = args.MaxLength;
            }

            var layout = new XStackLayout
            {
                Spacing  = 10,
                Children =
                {
                    new Label
                    {
                        LineBreakMode           = LineBreakMode.CharacterWrap,
                        TextColor               = DeviceInfo.Idiom == DeviceIdiom.Watch ? Color.FromRgb(255, 255, 255) : Application.AccentColor,
                        Text                    = args.Message,
                        HorizontalOptions       = LayoutOptions.Fill,
                        HorizontalTextAlignment = TextAlignment.Center,
#pragma warning disable CS0612 // Type or member is obsolete
                        FontSize = Device.GetNamedSize(NamedSize.Subtitle, typeof(Label)),
#pragma warning disable CS0612 // Type or member is obsolete
                    },
                    entry,
                }
            };

            layout.Parent = sender;
            var layoutrenderer = Platform.GetOrCreateRenderer(layout);

            var request = layout.Measure(DeviceInfo.Idiom == DeviceIdiom.Watch ? sender.Width * 0.7 : sender.Width, sender.Height);

            (layoutrenderer as ILayoutRenderer).RegisterOnLayoutUpdated();
            layoutrenderer.NativeView.MinimumHeight = Forms.ConvertToScaledPixel(request.Request.Height);
            layoutrenderer.NativeView.MinimumWidth  = Forms.ConvertToScaledPixel(request.Request.Width);

            prompt.Content = layoutrenderer.NativeView;

            var cancel = new EButton(prompt)
            {
                Text = args.Cancel
            };

            prompt.NegativeButton = cancel;
            cancel.Clicked       += (s, evt) =>
            {
                args.SetResult(null);
                prompt.Dismiss();
            };

            if (args.Accept != null)
            {
                var ok = new EButton(prompt)
                {
                    Text = args.Accept
                };
                prompt.NeutralButton = ok;
                ok.Clicked          += (s, evt) =>
                {
                    args.SetResult(entry.Text);
                    prompt.Dismiss();
                };
            }

            entry.Completed += (s, e) =>
            {
                args.SetResult(entry.Text);
                prompt.Dismiss();
            };

            prompt.BackButtonPressed += (s, evt) =>
            {
                prompt.Dismiss();
            };

            prompt.Show();

            _alerts.Add(prompt);
            prompt.Dismissed += (s, e) => _alerts.Remove(prompt);
        }