Пример #1
0
        public FlyoutItemTemplateSelector(INavigationView nv)
        {
            DefaultTemplate = new DataTemplate(() =>
            {
                var grid = new Grid
                {
                    HeightRequest = nv.GetFlyoutItemHeight(),
                    WidthRequest  = nv.GetFlyoutItemWidth()
                };

                ColumnDefinitionCollection columnDefinitions = new ColumnDefinitionCollection();
                columnDefinitions.Add(new ColumnDefinition {
                    Width = nv.GetFlyoutIconColumnSize()
                });
                columnDefinitions.Add(new ColumnDefinition {
                    Width = GridLength.Star
                });
                grid.ColumnDefinitions = columnDefinitions;

                var image = new Image
                {
                    VerticalOptions   = LayoutOptions.Center,
                    HorizontalOptions = LayoutOptions.Center,
                    HeightRequest     = nv.GetFlyoutIconSize(),
                    WidthRequest      = nv.GetFlyoutIconSize(),
                    Margin            = new Thickness(nv.GetFlyoutMargin(), 0, 0, 0),
                };
                image.SetBinding(Image.SourceProperty, new Binding("FlyoutIcon"));
                grid.Children.Add(image);

                var label = new Label
                {
                    FontSize = nv.GetFlyoutItemFontSize(),
                    VerticalTextAlignment = TextAlignment.Center,
                    Margin = new Thickness(nv.GetFlyoutMargin(), 0, 0, 0),
                };
                label.SetBinding(Label.TextProperty, new Binding("Title"));
                label.SetBinding(Label.TextColorProperty, new Binding("BackgroundColor", converter: new TextColorConverter(), source: grid));

                grid.Children.Add(label, 1, 0);

                var groups = new VisualStateGroupList();

                var commonGroup  = new VisualStateGroup();
                commonGroup.Name = "CommonStates";
                groups.Add(commonGroup);

                var normalState  = new VisualState();
                normalState.Name = "Normal";
                normalState.Setters.Add(new Setter
                {
                    Property = VisualElement.BackgroundColorProperty,
                    Value    = nv.GetTvFlyoutItemDefaultColor()
                });

                var focusedState  = new VisualState();
                focusedState.Name = "Focused";
                focusedState.Setters.Add(new Setter
                {
                    Property = VisualElement.BackgroundColorProperty,
                    Value    = nv.GetTvFlyoutItemFocusedColor()
                });

                var selectedState  = new VisualState();
                selectedState.Name = "Selected";
                selectedState.Setters.Add(new Setter
                {
                    Property = VisualElement.BackgroundColorProperty,
                    Value    = nv.GetTvFlyoutItemDefaultColor()
                });

                commonGroup.States.Add(normalState);
                commonGroup.States.Add(focusedState);
                commonGroup.States.Add(selectedState);

                VisualStateManager.SetVisualStateGroups(grid, groups);
                return(grid);
            });
        }