Ejemplo n.º 1
0
        // Generate Crossword Grid and Style
        private Grid GenerateCrosswordGrid(ObservableCollection <ObservableCollection <LetterCell> > board)
        {
            var crossWordGrid = new Grid()
            {
                ColumnSpacing = 1,
                RowSpacing    = 1,
                Padding       = 0
            };

            crossWordGrid.SetDynamicResource(Grid.BackgroundColorProperty, "backgroundGridColour");
            for (var i = 0; i < board.Count; i++)
            {
                crossWordGrid.RowDefinitions.Add(new RowDefinition {
                    Height = new GridLength(1, GridUnitType.Star)
                });
            }

            for (var i = 0; i < board[0].Count; i++)
            {
                crossWordGrid.ColumnDefinitions.Add(new ColumnDefinition {
                    Width = new GridLength(1, GridUnitType.Star)
                });
            }

            for (var i = 0; i < board.Count; i++)
            {
                for (var j = 0; j < board[i].Count; j++)
                {
                    var boxView = new BoxView()
                    {
                        CornerRadius = 2
                    };
                    boxView.BindingContext = board[i][j];
                    boxView.SetBinding(BoxView.BackgroundColorProperty, new Binding("BackgroundCellColour", BindingMode.OneWay));

                    var labelDefinitionLocation = new Label()
                    {
                        //Style = _resources["horizontalLabelStyle"] as Style
                    };
                    labelDefinitionLocation.BindingContext = board[i][j];
                    labelDefinitionLocation.SetBinding(Label.TextProperty, new Binding("DefinitionLocation", BindingMode.OneWay));
                    labelDefinitionLocation.SetDynamicResource(Label.StyleProperty, "orientationLabelStyle");
                    labelDefinitionLocation.SetDynamicResource(Label.TextColorProperty, "fontColour");

                    var entry = new CustomEntry()
                    {
                        MaxLength = 1,
                        HorizontalTextAlignment = TextAlignment.Center,
                    };
                    entry.Keyboard = Keyboard.Create(KeyboardFlags.CapitalizeCharacter);
                    //entry.Effects.Add(Effect.Resolve("FormsCommunityToolkit.Effects.EntryCapitalizeKeyboard"));

                    entry.BindingContext = board[i][j];
                    entry.SetBinding(CustomEntry.IsEnabledProperty, new Binding("IsEnabled", BindingMode.OneWay));
                    entry.SetBinding(CustomEntry.TextProperty, new Binding("LetterIn", BindingMode.TwoWay, new StringToCharConverter()));
                    entry.SetBinding(CustomEntry.TextColorProperty, new Binding("FontColour", BindingMode.OneWay));
                    entry.SetBinding(CustomEntry.FontAttributesProperty, new Binding("FontWeight", BindingMode.OneWay));
                    entry.SetDynamicResource(CustomEntry.StyleProperty, "orientationEntryStyle");
                    entry.TextChanged += (s, e) =>
                    {
                        if (IsCharUpper(e.NewTextValue) == null)
                        {
                            return;
                        }
                        if ((bool)IsCharUpper(e.NewTextValue))
                        {
                            LetterCell_TextChanged(entry, e);
                        }
                        else
                        {
                            entry.Text = e.NewTextValue.ToUpper();
                        }
                    };

                    entry.Focused += (s, e) =>
                    {
                        EntryFocused((LetterCell)e.VisualElement.BindingContext);
                    };


                    board[i][j].CellEntry = entry;
                    board[i][j].Pos       = new Tuple <int, int>(j, i);

                    var button = new Button()
                    {
                        BorderColor     = Color.Transparent,
                        BackgroundColor = Color.Transparent
                    };
                    button.BindingContext = board[i][j];
                    button.SetBinding(Button.IsEnabledProperty, new Binding("IsEnabled", BindingMode.OneWay));
                    button.Pressed += (s, e) =>
                    {
                        var letterCell = (LetterCell)button.BindingContext;

                        if (_buttonCount < 1)
                        {
                            Device.StartTimer(TimeSpan.FromMilliseconds(500), () => DoubleClick(letterCell));
                        }
                        _buttonCount++;
                    };

                    crossWordGrid.Children.Add(boxView, j, i);
                    crossWordGrid.Children.Add(labelDefinitionLocation, j, i);
                    crossWordGrid.Children.Add(entry, j, i);
                    crossWordGrid.Children.Add(button, j, i);
                }
            }

            return(crossWordGrid);
        }