Exemplo n.º 1
0
        IView CreateSampleCursorSelection()
        {
            var layout = new Microsoft.Maui.Controls.Layout2.GridLayout()
            {
                ColumnSpacing = 5, RowSpacing = 8, BackgroundColor = Colors.LightGreen
            };

            layout.AddRowDefinition(new RowDefinition()
            {
                Height = new GridLength(50)
            });

            layout.AddColumnDefinition(new ColumnDefinition()
            {
                Width = new GridLength(150)
            });
            layout.AddColumnDefinition(new ColumnDefinition()
            {
                Width = new GridLength(80)
            });
            layout.AddColumnDefinition(new ColumnDefinition()
            {
                Width = new GridLength(80)
            });

            Entry targetEntry = new() { Placeholder = "Selectable Text" };
            Entry forPosition = new() { Keyboard = Keyboard.Numeric, Placeholder = "CursorPos" };

            forPosition.TextChanged += (sender, args) =>
            {
                if (!int.TryParse(args.NewTextValue, out int newPos))
                {
                    return;
                }
                targetEntry.CursorPosition = newPos;
            };

            Entry forSelectionLen = new() { Keyboard = Keyboard.Numeric, Placeholder = "SelectionLen" };

            forSelectionLen.TextChanged += (sender, args) =>
            {
                if (!int.TryParse(args.NewTextValue, out int newSelectionLen))
                {
                    return;
                }
                targetEntry.SelectionLength = newSelectionLen;
            };

            layout.Add(targetEntry);
            layout.Add(forPosition);
            layout.SetColumn(forPosition, 1);
            layout.Add(forSelectionLen);
            layout.SetColumn(forSelectionLen, 2);

            return(layout);
        }

        IView CreateTransformations()
        {
            var label = new Button
            {
                BackgroundColor = Colors.Red,
                TextColor       = Colors.White,
                Text            = "Transformations",
            };

            var rotationSlider = new Slider
            {
                Minimum = -360,
                Maximum = 360
            };

            rotationSlider.ValueChanged += (sender, e) => label.Rotation = e.NewValue;

            var verticalStack = new VerticalStackLayout
            {
                rotationSlider,
                label,
            };

            return(verticalStack);
        }

        IView CreateAnimations()
        {
            var image = new Image {
                Source = "dotnet_bot.png", VerticalOptions = LayoutOptions.CenterAndExpand
            };
            var animateButton = new Button {
                Text = "Animate", VerticalOptions = LayoutOptions.End
            };

            animateButton.Clicked += async(sender, args) =>
            {
                await image.RotateTo(360, 2000);

                image.Rotation = 0;
            };

            var verticalStack = new VerticalStackLayout
            {
                image,
                animateButton,
            };

            return(verticalStack);
        }

        IView CreateShapes()
        {
            var ellipse = new Ellipse {
                Fill = new SolidColorBrush(Colors.Red), Stroke = new SolidColorBrush(Colors.Blue), StrokeThickness = 4, HeightRequest = 120, WidthRequest = 200
            };

            var line = new Line {
                X1 = 0, Y1 = 0, X2 = 80, Y2 = 90, Fill = new SolidColorBrush(Colors.Red), Stroke = new SolidColorBrush(Colors.Green), StrokeThickness = 4, StrokeDashArray = new double[] { 2, 2 }, HeightRequest = 200, WidthRequest = 200
            };
            Geometry pathData = (Geometry) new PathGeometryConverter().ConvertFromInvariantString("M15.999996,0L31.999999,13.000001 15.999996,26.199999 0,13.000001z");
            var      path     = new Microsoft.Maui.Controls.Shapes.Path {
                Data = pathData, Fill = new SolidColorBrush(Colors.Pink), Stroke = new SolidColorBrush(Colors.Red), StrokeThickness = 1, HeightRequest = 100, WidthRequest = 100
            };

            var polyline = new Polyline {
                Points = new[] { new Point(10, 10), new Point(100, 50), new Point(50, 90) }, Stroke = new SolidColorBrush(Colors.Black), StrokeThickness = 2, StrokeDashArray = new double[] { 1, 1, 1, 1 }, HeightRequest = 100, WidthRequest = 100
            };

            var polygon = new Polygon {
                Points = new[] { new Point(10, 10), new Point(100, 50), new Point(50, 90) }, Fill = new SolidColorBrush(Colors.LightBlue), Stroke = new SolidColorBrush(Colors.Black), StrokeThickness = 2, StrokeDashArray = new double[] { 2, 2 }, HeightRequest = 100, WidthRequest = 100
            };

            var rectangle = new Microsoft.Maui.Controls.Shapes.Rectangle {
                RadiusX = 12, RadiusY = 6, Fill = new LinearGradientBrush(new GradientStopCollection {
                    new GradientStop(Colors.Green, 0), new GradientStop(Colors.Blue, 1)
                }, new Point(0, 0), new Point(1, 0)), Stroke = new SolidColorBrush(Colors.Purple), StrokeThickness = 8, StrokeDashArray = new float[] { 2, 2 }, HeightRequest = 120, WidthRequest = 200
            };

            var verticalStack = new VerticalStackLayout
            {
                ellipse,
                line,
                path,
                polyline,
                polygon,
                rectangle
            };

            return(verticalStack);
        }

        void AddTextResizeDemo(Microsoft.Maui.ILayout layout)
        {
            var resizeTestButton = new Button {
                Text = "Resize Test"
            };

            var resizeTestLabel = new Label {
                Text = "Short Text", BackgroundColor = Colors.LightBlue, HorizontalOptions = LayoutOptions.Start
            };
            var explicitWidthTestLabel = new Label {
                Text = "Short Text", BackgroundColor = Colors.LightGreen, WidthRequest = 200
            };
            var widthAndHeightTestLabel = new Label {
                Text = "Short Text", BackgroundColor = Colors.MediumSeaGreen, WidthRequest = 150, HeightRequest = 40
            };

            resizeTestButton.Clicked += (sender, args) =>
            {
                if (resizeTestLabel.Text == "Short Text")
                {
                    resizeTestLabel.Text         = LoremIpsum;
                    explicitWidthTestLabel.Text  = LoremIpsum;
                    widthAndHeightTestLabel.Text = LoremIpsum;
                }
                else
                {
                    resizeTestLabel.Text         = "Short Text";
                    explicitWidthTestLabel.Text  = "Short Text";
                    widthAndHeightTestLabel.Text = "Short Text";
                }
            };

            layout.Add(resizeTestButton);
            layout.Add(resizeTestLabel);
            layout.Add(widthAndHeightTestLabel);
            layout.Add(explicitWidthTestLabel);
        }

        void SetupVisibilityTest()
        {
            var layout = new VerticalStackLayout()
            {
                BackgroundColor = Colors.BurlyWood
            };

            var button1 = new Button {
                Text = "Controls", Margin = new Thickness(0, 40)
            };

            var button2 = new Button {
                Text = "MAUI"
            };

            var controlsLabel = new Label {
                Text = "Controls Label"
            };

            controlsLabel.IsVisible = true;

            var alwaysVisible = new Label {
                Text = "Always visible"
            };

            var mauiLabel = new VisibilityLabel()
            {
                Text = "Core Label"
            };

            button1.Clicked += (sender, args) =>
            {
                controlsLabel.IsVisible = !controlsLabel.IsVisible;
            };

            button2.Clicked += (sender, args) =>
            {
                switch ((mauiLabel as IFrameworkElement).Visibility)
                {
                case Visibility.Visible:
                    mauiLabel.SetVisibility(Visibility.Hidden);
                    break;

                case Visibility.Hidden:
                    mauiLabel.SetVisibility(Visibility.Collapsed);
                    break;

                case Visibility.Collapsed:
                    mauiLabel.SetVisibility(Visibility.Visible);
                    break;
                }
            };

            layout.Add(button1);
            layout.Add(button2);
            layout.Add(controlsLabel);
            layout.Add(mauiLabel);
            layout.Add(alwaysVisible);

            Content = layout;
        }

        class TestDrawable : IDrawable
        {
Exemplo n.º 2
0
        public LayoutUpdatesPage()
        {
            var root = new GridLayout()
            {
                Margin = 40, BackgroundColor = Colors.Beige
            };

            root.RowDefinitions = new RowDefinitionCollection()
            {
                new RowDefinition()
                {
                    Height = GridLength.Auto
                },
                new RowDefinition()
                {
                    Height = GridLength.Auto
                },
                new RowDefinition()
                {
                    Height = GridLength.Auto
                },
                new RowDefinition()
                {
                    Height = GridLength.Auto
                }
            };

            root.ColumnDefinitions = new ColumnDefinitionCollection()
            {
                new ColumnDefinition()
                {
                    Width = GridLength.Star
                },
                new ColumnDefinition()
                {
                    Width = GridLength.Star
                },
            };

            var addButton = new Button {
                Text = "Add"
            };

            root.Add(addButton);

            var removeButton = new Button {
                Text = "Remove"
            };

            root.Add(removeButton);
            root.SetColumn(removeButton, 1);

            var insertButton = new Button {
                Text = "Insert"
            };

            root.Add(insertButton);
            root.SetRow(insertButton, 1);

            var clearButton = new Button {
                Text = "Clear"
            };

            root.Add(clearButton);
            root.SetColumn(clearButton, 1);
            root.SetRow(clearButton, 1);

            var updateButton = new Button {
                Text = "Update"
            };

            root.Add(updateButton);
            root.SetRow(updateButton, 2);

            var stack = new VerticalStackLayout();

            double shapeWidth          = 200;
            double shapeHeight         = 80;
            double overlap             = -20;
            double leftMarginIncrement = 20;

            var r1 = new Shapes.Rectangle()
            {
                WidthRequest = shapeWidth, HeightRequest = shapeHeight, Fill = new SolidColorBrush(NextColor()), Margin = new Thickness(0, 0, 0, 0)
            };
            var r2 = new Shapes.Rectangle()
            {
                WidthRequest = shapeWidth, HeightRequest = shapeHeight, Fill = new SolidColorBrush(NextColor()), Margin = new Thickness(leftMarginIncrement, overlap, 0, 0)
            };

            stack.Add(r1);
            stack.Add(r2);

            root.Add(stack);
            root.SetRow(stack, 3);
            root.SetColumnSpan(stack, 2);

            addButton.Clicked += (sender, args) =>
            {
                var left = leftMarginIncrement * stack.Count;
                var rect = new Shapes.Rectangle()
                {
                    WidthRequest = shapeWidth, HeightRequest = shapeHeight, Fill = new SolidColorBrush(NextColor()), Margin = new Thickness(left, overlap, 0, 0)
                };
                stack.Add(rect);
            };

            insertButton.Clicked += (sender, args) =>
            {
                if (stack.Count < 2)
                {
                    return;
                }

                var left = leftMarginIncrement * stack.Count;

                var rect = new Shapes.Rectangle()
                {
                    WidthRequest = shapeWidth, HeightRequest = shapeHeight, Fill = new SolidColorBrush(NextColor()), Margin = new Thickness(left, overlap, 0, 0)
                };
                stack.Insert(1, rect);
            };

            clearButton.Clicked += (sender, args) =>
            {
                stack.Clear();
            };

            removeButton.Clicked += (sender, args) =>
            {
                if (stack.Count > 0)
                {
                    stack.RemoveAt(stack.Count - 1);
                }
            };

            updateButton.Clicked += (sender, args) =>
            {
                if (stack.Count > 0)
                {
                    var left = leftMarginIncrement * stack.Count;
                    var rect = new Shapes.Rectangle()
                    {
                        WidthRequest = shapeWidth, HeightRequest = shapeHeight, Fill = new SolidColorBrush(NextColor()), Margin = new Thickness(left, overlap, 0, 0)
                    };
                    stack[0] = rect;
                }
            };

            Content = root;
        }
Exemplo n.º 3
0
        void UpdateBorder()
        {
            var startColor = GetColorFromString(BorderStartColor.Text);
            var endColor   = GetColorFromString(BorderEndColor.Text);

            BorderStartColor.BackgroundColor = startColor;
            BorderEndColor.BackgroundColor   = endColor;

            Shape borderShape = null;

            switch (BorderShapePicker.SelectedIndex)
            {
            case 0:
                borderShape = new Microsoft.Maui.Controls.Shapes.Rectangle();
                break;

            case 1:
                borderShape = new RoundRectangle
                {
                    CornerRadius = new CornerRadius(TopLeftCornerSlider.Value, TopRightCornerSlider.Value,
                                                    BottomLeftCornerSlider.Value, BottomRightCornerSlider.Value)
                };
                break;

            case 2:
                borderShape = new Ellipse();
                break;
            }

            BorderView.StrokeShape = borderShape;

            BorderView.Stroke = new LinearGradientBrush
            {
                StartPoint    = new Point(0, 0),
                EndPoint      = new Point(1, 0),
                GradientStops = new GradientStopCollection
                {
                    new Microsoft.Maui.Controls.GradientStop {
                        Color = startColor, Offset = 0.0f
                    },
                    new Microsoft.Maui.Controls.GradientStop {
                        Color = endColor, Offset = 0.9f
                    }
                }
            };

            BorderView.StrokeThickness = BorderWidthSlider.Value;

            var borderDashArrayString = BorderDashArrayEntry.Text;

            if (string.IsNullOrEmpty(borderDashArrayString))
            {
                BorderView.StrokeDashArray = new DoubleCollection();
            }
            else
            {
                var doubleCollectionConverter = new DoubleCollectionConverter();
                var doubleCollection          = (DoubleCollection)doubleCollectionConverter.ConvertFromString(borderDashArrayString);
                BorderView.StrokeDashArray = doubleCollection;
            }

            BorderView.StrokeDashOffset = BorderDashOffsetSlider.Value;

            PenLineJoin borderLineJoin = PenLineJoin.Miter;

            switch (BorderLineJoinPicker.SelectedIndex)
            {
            case 0:
                borderLineJoin = PenLineJoin.Miter;
                break;

            case 1:
                borderLineJoin = PenLineJoin.Round;
                break;

            case 2:
                borderLineJoin = PenLineJoin.Bevel;
                break;
            }

            BorderView.StrokeLineJoin = borderLineJoin;

            PenLineCap borderLineCap = PenLineCap.Flat;

            switch (BorderLineCapPicker.SelectedIndex)
            {
            case 0:
                borderLineCap = PenLineCap.Flat;
                break;

            case 1:
                borderLineCap = PenLineCap.Round;
                break;

            case 2:
                borderLineCap = PenLineCap.Square;
                break;
            }

            BorderView.StrokeLineCap = borderLineCap;
        }