コード例 #1
0
        public ColorScrollPage()
        {
            // Define a 2 x 2 Grid that will be modified for portrait
            //      or landscape in SizeChanged handler.
            mainGrid = new Grid
            {
                ColumnDefinitions = 
                {
                    new ColumnDefinition(),
                    new ColumnDefinition()
                },
                RowDefinitions = 
                {
                    new RowDefinition(),
                    new RowDefinition()
                }
            };

            // Put all Labels and Sliders in StackLayout.
            //  Wait until SizeChanged to set row and column.
            controllersStack = new StackLayout();
            mainGrid.Children.Add(controllersStack);

            // Also wait until SizeChanged to set BoxView row and column.
            boxView = new BoxView 
            {
                Color = currentColor
            };
            mainGrid.Children.Add(boxView);

            // Assemble 3-column Grid to display color options.
            Grid radioGrid = new Grid
            {
                VerticalOptions = LayoutOptions.CenterAndExpand,
            };

            string[] modeLabels = { "Hex RGB", "Float RGB", "HSL" };

            foreach (ColorMode colorMode in Enum.GetValues(typeof(ColorMode)))
            {
                // Define a Label to be used as a radio button.
                Label radioLabel = new Label
                {
                    Text = modeLabels[(int)colorMode],
                    StyleId = colorMode.ToString(),
                    XAlign = TextAlignment.Center,
                    Opacity = 0.5
                };
                radioLabel.AddRadioToggler(OnRadioLabelToggled);

                // Set default item.
                radioLabel.SetRadioState(colorMode == ColorMode.RgbHex);

                // Add it to the Grid.
                radioGrid.Children.AddHorizontal(radioLabel);

                // Set the column width to "star" to equally space them.
                radioGrid.ColumnDefinitions.Add(new ColumnDefinition
                {
                    Width = new GridLength(1, GridUnitType.Star)
                });
            }
            controllersStack.Children.Add(radioGrid);

            // Create Labels and Sliders for the three color components.
            for (int component = 0; component < 3; component++)
            {
                labels[component] = new Label
                {
                    XAlign = TextAlignment.Center
                };
                controllersStack.Children.Add(labels[component]);

                // Set same ValueChanged handler for all sliders.
                sliders[component] = new Slider();
                sliders[component].ValueChanged += OnSliderValueChanged;
                controllersStack.Children.Add(sliders[component]);
            }

            // Build page.
            this.Padding = new Thickness(0, Device.OnPlatform(20, 0, 0), 0, 0);
            this.Content = mainGrid;

            // Set SizeChanged handler.
            SizeChanged += OnPageSizeChanged;

            // Initialize Slider values.
            SetSlidersAndBindings ();
        }