Example #1
0
        /// <summary>
        /// Called when the binding context for this cell is changed.
        /// </summary>
        /// <param name="sender">The object sending the event.</param>
        /// <param name="e">The event parameters.</param>
        private void ViewCell_BindingContextChanged(object sender, EventArgs e)
        {
            // Since we can not used nested ListViews and we do not know the number of senses a priori, populate the
            // details panel here in code.
            // We need to put this in an event handler because, when the constructor is called, the BindingContext is
            // null
            CollinsWord word = (CollinsWord)this.BindingContext;

            if (word != null)
            {
                for (int senseCounter = 0; senseCounter < word.Senses.Count; ++senseCounter)
                {
                    this.DetailsPanel.RowDefinitions.Add(new RowDefinition()
                    {
                        Height = GridLength.Auto
                    });

                    Grid definitionGrid = new Grid();
                    definitionGrid.BackgroundColor = CollinsViewCell.backgroundColors[senseCounter % CollinsViewCell.backgroundColors.Length];

                    definitionGrid.ColumnDefinitions.Add(new ColumnDefinition()
                    {
                        Width = GridLength.Auto
                    });
                    definitionGrid.ColumnDefinitions.Add(new ColumnDefinition()
                    {
                        Width = GridLength.Auto
                    });

                    int lastRow = 0;

                    if (word.Senses[senseCounter].Definitions.Count > 0)
                    {
                        Binding definitionBinding = new Binding("Senses[" + senseCounter + "].Definitions")
                        {
                            Converter = new StringListToHumanReadableListConverter()
                        };
                        Label definitionLabel = new Label();
                        definitionLabel.FontSize      = 14;
                        definitionLabel.LineBreakMode = LineBreakMode.WordWrap;
                        definitionLabel.SetBinding(Label.TextProperty, definitionBinding);

                        definitionGrid.RowDefinitions.Add(new RowDefinition()
                        {
                            Height = GridLength.Auto
                        });
                        definitionGrid.Children.Add(
                            new Label()
                        {
                            FontSize       = 14,
                            FontAttributes = FontAttributes.Bold,
                            Text           = Properties.Resources.CollinsViewCell_Definition,
                        },
                            0,
                            lastRow);
                        definitionGrid.Children.Add(definitionLabel, 1, lastRow);

                        ++lastRow;
                    }

                    if (word.Senses[senseCounter].Examples.Count > 0)
                    {
                        Binding examplesBinding = new Binding("Senses[" + senseCounter + "].Examples")
                        {
                            Converter = new StringListToHumanReadableListConverter()
                        };
                        Label examplesLabel = new Label();
                        examplesLabel.FontSize      = 14;
                        examplesLabel.LineBreakMode = LineBreakMode.WordWrap;
                        examplesLabel.SetBinding(Label.TextProperty, examplesBinding);

                        definitionGrid.RowDefinitions.Add(new RowDefinition()
                        {
                            Height = GridLength.Auto
                        });
                        definitionGrid.Children.Add(
                            new Label()
                        {
                            FontSize       = 14,
                            FontAttributes = FontAttributes.Bold,
                            Text           = Properties.Resources.CollinsViewCell_Example
                        },
                            0,
                            lastRow);
                        definitionGrid.Children.Add(examplesLabel, 1, lastRow);

                        ++lastRow;
                    }

                    if (word.Senses[senseCounter].Related.Count > 0)
                    {
                        Binding relatedBinding = new Binding("Senses[" + senseCounter + "].Related")
                        {
                            Converter = new CollinsJsonLinkedWordListToHumanReadableStringConverter()
                        };
                        Label relatedLabel = new Label();
                        relatedLabel.FontSize      = 14;
                        relatedLabel.LineBreakMode = LineBreakMode.WordWrap;
                        relatedLabel.SetBinding(Label.TextProperty, relatedBinding);

                        definitionGrid.RowDefinitions.Add(new RowDefinition()
                        {
                            Height = GridLength.Auto
                        });
                        definitionGrid.Children.Add(
                            new Label()
                        {
                            FontSize       = 14,
                            FontAttributes = FontAttributes.Bold,
                            Text           = Properties.Resources.CollinsViewCell_Related
                        },
                            0,
                            lastRow);
                        definitionGrid.Children.Add(relatedLabel, 1, lastRow);

                        ++lastRow;
                    }

                    if (word.Senses[senseCounter].SeeAlso.Count > 0)
                    {
                        Binding seeAlsoBinding = new Binding("Senses[" + senseCounter + "].SeeAlso")
                        {
                            Converter = new CollinsJsonLinkedWordListToHumanReadableStringConverter()
                        };
                        Label seeAlsoLabel = new Label();
                        seeAlsoLabel.FontSize      = 14;
                        seeAlsoLabel.LineBreakMode = LineBreakMode.WordWrap;
                        seeAlsoLabel.SetBinding(Label.TextProperty, seeAlsoBinding);

                        definitionGrid.RowDefinitions.Add(new RowDefinition()
                        {
                            Height = GridLength.Auto
                        });
                        definitionGrid.Children.Add(
                            new Label()
                        {
                            FontSize       = 14,
                            FontAttributes = FontAttributes.Bold,
                            Text           = Properties.Resources.CollinsViewCell_SeeAlso
                        },
                            0,
                            lastRow);
                        definitionGrid.Children.Add(seeAlsoLabel, 1, lastRow);

                        ++lastRow;
                    }

                    this.DetailsPanel.Children.Add(definitionGrid, 0, senseCounter);
                }
            }
        }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CollinsWordView"/> class.
        /// </summary>
        /// <param name="word">The instance of <see cref="CollinsWord"/> containing the word to represent.</param>
        public CollinsWordView(CollinsWord word)
            : base()
        {
            this.BackgroundColor   = Color.Aqua;
            this.Padding           = 1;
            this.Margin            = 1;
            this.word              = word;
            this.HorizontalOptions = LayoutOptions.FillAndExpand;
            this.VerticalOptions   = LayoutOptions.Start;

            for (int senseNum = 0; senseNum < this.word.Senses.Count; ++senseNum)
            {
                Grid senseGrid = new Grid()
                {
                    ColumnDefinitions = new ColumnDefinitionCollection()
                    {
                        { new ColumnDefinition()
                          {
                              Width = GridLength.Auto
                          } },
                        { new ColumnDefinition()
                          {
                              Width = GridLength.Star
                          } },
                    },
                    RowDefinitions = new RowDefinitionCollection()
                    {
                        { new RowDefinition()
                          {
                              Height = GridLength.Auto
                          } },
                        { new RowDefinition()
                          {
                              Height = GridLength.Auto
                          } }
                    },
                    HorizontalOptions = LayoutOptions.FillAndExpand,
                    BackgroundColor   = CollinsMultipleWordsWrapper._bkgColors[senseNum % 6]
                };

                int rowCounter = 0;
                {
                    Label titleLabel = new Label
                    {
                        Text              = "[" + new PartOfSpeechToStringConverter().Convert(this.word.PartOfSpeech, typeof(string), null, CultureInfo.CurrentCulture) + "] Sense #" + (senseNum + 1),
                        VerticalOptions   = LayoutOptions.CenterAndExpand,
                        HorizontalOptions = LayoutOptions.Start,
                        FontAttributes    = FontAttributes.Bold,
                        FontSize          = 12d
                    };

                    senseGrid.Children.Add(titleLabel, 0, rowCounter);
                    Grid.SetColumnSpan(titleLabel, 2);

                    CollinsWordDefinitionSense currentSense = this.word.Senses[senseNum];
                    for (int definitionNum = 0; definitionNum < currentSense.Definitions.Count; ++definitionNum)
                    {
                        ++rowCounter;
                        senseGrid.Children.Add(
                            new Label
                        {
                            Text              = "Definition #" + (definitionNum + 1),
                            VerticalOptions   = LayoutOptions.CenterAndExpand,
                            HorizontalOptions = LayoutOptions.Start,
                            FontAttributes    = FontAttributes.Bold,
                            FontSize          = 12d
                        },
                            0,
                            rowCounter);
                        senseGrid.Children.Add(
                            new Label
                        {
                            Text              = currentSense.Definitions[definitionNum],
                            VerticalOptions   = LayoutOptions.CenterAndExpand,
                            HorizontalOptions = LayoutOptions.Start,
                            FontAttributes    = FontAttributes.Bold,
                            FontSize          = 12d
                        },
                            1,
                            rowCounter);
                    }

                    for (int exampleNum = 0; exampleNum < currentSense.Examples.Count; ++exampleNum)
                    {
                        rowCounter++;
                        senseGrid.Children.Add(
                            new Label
                        {
                            Text              = "Example #" + (exampleNum + 1),
                            VerticalOptions   = LayoutOptions.CenterAndExpand,
                            HorizontalOptions = LayoutOptions.Start,
                            FontAttributes    = FontAttributes.Bold,
                            FontSize          = 12d
                        },
                            0,
                            rowCounter);
                        senseGrid.Children.Add(
                            new Label
                        {
                            Text              = currentSense.Examples[exampleNum],
                            VerticalOptions   = LayoutOptions.CenterAndExpand,
                            HorizontalOptions = LayoutOptions.Start,
                            FontAttributes    = FontAttributes.Bold,
                            FontSize          = 12d
                        },
                            1,
                            rowCounter);
                    }
                }

                this.Children.Add(senseGrid);
            }
        }