Beispiel #1
0
        protected override void Render()
        {
            var native  = Native;
            var element = Element;

            // Visibility
            if (element.Visibility == Visibility.Visible)
            {
                // Icon
                var iconUri = (element.Icon as BitmapIcon)?.UriSource;

                if (iconUri != null)
                {
                    native.NavigationIcon = DrawableHelper.FromUri(iconUri);
                }

                // Foreground
                var foreground        = (element.Foreground as SolidColorBrush);
                var foregroundOpacity = foreground?.Opacity ?? 0;

                if (FeatureConfiguration.AppBarButton.EnableBitmapIconTint)
                {
                    var foregroundColor = foreground?.Color;
                    if (native.NavigationIcon != null)
                    {
                        if (foreground != null)
                        {
                            DrawableCompat.SetTint(native.NavigationIcon, (Android.Graphics.Color)foregroundColor);
                        }
                        else
                        {
                            DrawableCompat.SetTintList(native.NavigationIcon, null);
                        }
                    }
                }

                // Label
                native.NavigationContentDescription = element.Label;

                // Opacity
                var opacity      = element.Opacity;
                var finalOpacity = foregroundOpacity * opacity;
                var alpha        = (int)(finalOpacity * 255);
                native.NavigationIcon?.SetAlpha(alpha);
            }
            else
            {
                native.NavigationIcon = null;
                native.NavigationContentDescription = null;
            }
        }
Beispiel #2
0
        protected override void Render()
        {
            // Visibility
            if (Element.Visibility == Visibility.Visible)
            {
                // Icon
                var iconUri = (Element.Icon as BitmapIcon)?.UriSource;
                Native.NavigationIcon = DrawableHelper.FromUri(iconUri);

                // Foreground
                var foreground        = (Element.Foreground as SolidColorBrush);
                var foregroundColor   = foreground?.Color;
                var foregroundOpacity = foreground?.Opacity ?? 0;
                if (Native.NavigationIcon != null)
                {
                    if (foreground != null)
                    {
                        DrawableCompat.SetTint(Native.NavigationIcon, (Android.Graphics.Color)foregroundColor);
                    }
                    else
                    {
                        DrawableCompat.SetTintList(Native.NavigationIcon, null);
                    }
                }

                // Label
                Native.NavigationContentDescription = Element.Label;

                // Opacity
                var opacity      = Element.Opacity;
                var finalOpacity = foregroundOpacity * opacity;
                var alpha        = (int)(finalOpacity * 255);
                Native.NavigationIcon?.SetAlpha(alpha);
            }
            else
            {
                Native.NavigationIcon = null;
                Native.NavigationContentDescription = null;
            }
        }
        protected override void Render()
        {
            // Content
            Native.Title                 = Element.Content as string;
            _contentContainer.Child      = Element.Content as View;
            _contentContainer.Visibility = Element.Content is View
                                ? Visibility.Visible
                                : Visibility.Collapsed;

            // CommandBarExtensions.Subtitle
            Native.Subtitle = Element.GetValue(SubtitleProperty) as string;

            // Background
            var backgroundColor = (Element.Background as SolidColorBrush)?.ColorWithOpacity;

            if (backgroundColor != null)
            {
                Native.SetBackgroundColor((Android.Graphics.Color)backgroundColor);
            }
            else
            {
                Native.Background = _originalBackground ?? new ColorDrawable(Color.FromArgb(255, 250, 250, 250));
            }

            // Foreground
            var foregroundColor = (Element.Foreground as SolidColorBrush)?.ColorWithOpacity;

            if (foregroundColor != null)
            {
                Native.SetTitleTextColor((Android.Graphics.Color)foregroundColor);
            }
            else
            {
                Native.SetTitleTextColor(_originalTitleTextColor.Value);
            }

            // PrimaryCommands & SecondaryCommands
            var currentMenuItemIds  = GetMenuItems(Native.Menu).Select(i => i.ItemId);
            var intendedMenuItemIds = Element.PrimaryCommands
                                      .Concat(Element.SecondaryCommands)
                                      .OfType <AppBarButton>()
                                      .Select(i => i.GetHashCode());

            if (!currentMenuItemIds.SequenceEqual(intendedMenuItemIds))
            {
                Native.Menu.Clear();
                foreach (var command in Element.PrimaryCommands.Concat(Element.SecondaryCommands).OfType <AppBarButton>())
                {
                    var menuItem = Native.Menu.Add(0, command.GetHashCode(), Menu.None, null);

                    var renderer = command.GetRenderer(() => new AppBarButtonRenderer(command));
                    renderer.Native = menuItem;

                    // This ensures that Behaviors expecting this button to be in the logical tree work.
                    command.SetParent(Element);
                }
            }

            // CommandBarExtensions.NavigationCommand
            var navigationCommand = Element.GetValue(NavigationCommandProperty) as AppBarButton;

            if (navigationCommand != null)
            {
                var renderer = navigationCommand.GetRenderer(() => new NavigationAppBarButtonRenderer(navigationCommand));
                renderer.Native = Native;

                // This ensures that Behaviors expecting this button to be in the logical tree work.
                navigationCommand.SetParent(Element);
            }
            // CommandBarExtensions.BackButtonVisibility
            else if ((Visibility)Element.GetValue(BackButtonVisibilityProperty) == Visibility.Visible)
            {
                // CommandBarExtensions.BackButtonIcon
                if (Element.GetValue(BackButtonIconProperty) is BitmapIcon bitmapIcon)
                {
                    Native.NavigationIcon = DrawableHelper.FromUri(bitmapIcon.UriSource);
                }
                else
                {
                    Native.NavigationIcon = new Android.Support.V7.Graphics.Drawable.DrawerArrowDrawable(ContextHelper.Current)
                    {
                        // 0 = menu icon
                        // 1 = back icon
                        Progress = 1,
                    };
                }

                // CommandBarExtensions.BackButtonForeground
                var backButtonForeground = (Element.GetValue(BackButtonForegroundProperty) as SolidColorBrush)?.ColorWithOpacity;
                if (backButtonForeground != null)
                {
                    switch (Native.NavigationIcon)
                    {
                    case Android.Support.V7.Graphics.Drawable.DrawerArrowDrawable drawerArrowDrawable:
                        drawerArrowDrawable.Color = (Android.Graphics.Color)backButtonForeground;
                        break;

                    case Drawable drawable:
                        DrawableCompat.SetTint(drawable, (Android.Graphics.Color)backButtonForeground);
                        break;
                    }
                }
            }
            else
            {
                Native.NavigationIcon = null;
                Native.NavigationContentDescription = null;
            }

            // Padding
            var physicalPadding = Element.Padding.LogicalToPhysicalPixels();

            Native.SetPadding(
                (int)physicalPadding.Left,
                (int)physicalPadding.Top,
                (int)physicalPadding.Right,
                (int)physicalPadding.Bottom
                );

            // Opacity
            Native.Alpha = (float)Element.Opacity;
        }
        protected override void Render()
        {
            // CommandBar::PrimaryCommands -> !IsInOverflow -> AsAction.Never -> displayed directly on command bar
            // CommandBar::SecondaryCommands -> IsInOverflow -> AsAction.Awalys -> (displayed as flyout menu items under [...])

            var native  = Native;
            var element = Element;

            // IsInOverflow
            var showAsAction = element.IsInOverflow
                                ? ShowAsAction.Never
                                : ShowAsAction.Always;

            native.SetShowAsAction(showAsAction);

            // (Icon ?? Content) and Label
            if (element.IsInOverflow)
            {
                native.SetActionView(null);
                native.SetIcon(null);
                native.SetTitle(element.Label);
            }
            else if (element.Icon != null)
            {
                switch (element.Icon)
                {
                case BitmapIcon bitmap:
                    var drawable = DrawableHelper.FromUri(bitmap.UriSource);
                    native.SetIcon(drawable);
                    break;

                case FontIcon font:                         // not supported
                case PathIcon path:                         // not supported
                case SymbolIcon symbol:                     // not supported
                default:
                    this.Log().WarnIfEnabled(() => $"{GetType().Name ?? "FontIcon, PathIcon and SymbolIcon"} are not supported. Use BitmapIcon instead with UriSource.");
                    native.SetIcon(null);
                    break;
                }
                native.SetActionView(null);
                native.SetTitle(null);
            }
            else
            {
                switch (element.Content)
                {
                case string text:
                    native.SetIcon(null);
                    native.SetActionView(null);
                    native.SetTitle(text);
                    break;

                case FrameworkElement fe:
                    var currentParent = element.GetParent();
                    _appBarButtonWrapper.Child = element;

                    //Restore the original parent if any, as we
                    // want the DataContext to flow properly from the
                    // CommandBar.
                    element.SetParent(currentParent);

                    native.SetIcon(null);
                    native.SetActionView(fe.Visibility == Visibility.Visible ? _appBarButtonWrapper : null);
                    native.SetTitle(null);
                    break;

                default:
                    native.SetIcon(null);
                    native.SetActionView(null);
                    native.SetTitle(null);
                    break;
                }
            }

            // IsEnabled
            native.SetEnabled(element.IsEnabled);
            // According to the Material Design guidelines, the opacity inactive icons should be:
            // - Light background: 38%
            // - Dark background: 50%
            // Source: https://material.io/guidelines/style/icons.html
            // For lack of a reliable way to identify whether the background is light or dark,
            // we'll go with 50% opacity until this no longer satisfies projects requirements.
            var isEnabledOpacity = (element.IsEnabled ? 1.0 : 0.5);

            // Visibility
            native.SetVisible(element.Visibility == Visibility.Visible);

            // Foreground
            var foreground        = element.Foreground as SolidColorBrush;
            var foregroundColor   = foreground?.Color;
            var foregroundOpacity = foreground?.Opacity ?? 0;

            if (native.Icon != null)
            {
                if (foreground != null)
                {
                    DrawableCompat.SetTint(native.Icon, (Android.Graphics.Color)foregroundColor);
                }
                else
                {
                    DrawableCompat.SetTintList(native.Icon, null);
                }
            }

            // Background
            var backgroundColor = (element.Background as SolidColorBrush)?.ColorWithOpacity;

            if (backgroundColor != null)
            {
                _appBarButtonWrapper.SetBackgroundColor((Android.Graphics.Color)backgroundColor);
            }

            // Opacity
            var opacity      = element.Opacity;
            var finalOpacity = isEnabledOpacity * foregroundOpacity * opacity;
            var alpha        = (int)(finalOpacity * 255);

            native.Icon?.SetAlpha(alpha);
        }
        protected override void Render()
        {
            // IsInOverflow
            var showAsAction = Element.IsInOverflow
                                ? ShowAsAction.Never
                                : ShowAsAction.Always;

            Native.SetShowAsAction(showAsAction);

            // Label / String Content
            Native.SetTitle(Element.Label ?? (Element.Content is string contentLabel ? contentLabel : string.Empty));

            // Content & Icon
            if (!Element.IsInOverflow)             // We don't want to consider Icon or Content in overflow
            {
                // Icon
                switch (Element.Icon)
                {
                case BitmapIcon bitmap:
                    var drawable = DrawableHelper.FromUri(bitmap.UriSource);
                    Native.SetIcon(drawable);
                    break;

                default:
                    // Custom Content
                    switch (Element.Content)
                    {
                    case FrameworkElement content:
                        var currentParent = Element.GetParent();
                        _appBarButtonWrapper.Child = Element;

                        // Restore the original parent if any, as we
                        // want the DataContext to flow properly from the
                        // CommandBar.
                        Element.SetParent(currentParent);
                        if (content.Visibility == Visibility.Visible)
                        {
                            Native.SetActionView(_appBarButtonWrapper);
                        }
                        break;
                    }
                    break;
                }
            }

            // IsEnabled
            Native.SetEnabled(Element.IsEnabled);
            // According to the Material Design guidelines, the opacity inactive icons should be:
            // - Light background: 38%
            // - Dark background: 50%
            // Source: https://material.io/guidelines/style/icons.html
            // For lack of a reliable way to identify whether the background is light or dark,
            // we'll go with 50% opacity until this no longer satisfies projects requirements.
            var isEnabledOpacity = (Element.IsEnabled ? 1.0 : 0.5);

            // Visibility
            Native.SetVisible(Element.Visibility == Visibility.Visible);

            // Foreground
            var foreground        = Element.Foreground as SolidColorBrush;
            var foregroundColor   = foreground?.Color;
            var foregroundOpacity = foreground?.Opacity ?? 0;

            if (Native.Icon != null)
            {
                if (foreground != null)
                {
                    DrawableCompat.SetTint(Native.Icon, (Android.Graphics.Color)foregroundColor);
                }
                else
                {
                    DrawableCompat.SetTintList(Native.Icon, null);
                }
            }

            // Background
            var backgroundColor = (Element.Background as SolidColorBrush)?.ColorWithOpacity;

            if (backgroundColor != null)
            {
                _appBarButtonWrapper.SetBackgroundColor((Android.Graphics.Color)backgroundColor);
            }

            // Opacity
            var opacity      = Element.Opacity;
            var finalOpacity = isEnabledOpacity * foregroundOpacity * opacity;
            var alpha        = (int)(finalOpacity * 255);

            Native.Icon?.SetAlpha(alpha);
        }