From 75f112400223ea628ee008d311b7d5d54d98eafe Mon Sep 17 00:00:00 2001 From: Samantha Houts Date: Mon, 18 Apr 2016 09:46:51 -0700 Subject: BarBackgroundColor and BarTextColor on TabbedPage (#96) * [Core] Add properties to TabbedPage * [Controls] Add properties to test page * [iOS] Added BarBackgroundColor & BarTextColor to TabbedPage * [A] Added BarBackgroundColor & BarTextColor to TabbedPage * [UWP] Added BarBackgroundColor & BarTextColor to TabbedPage * [WinRT] Format file * [WinRT] Added BarBackgroundColor & BarTextColor to TabbedPage * [Docs] Updated docs --- Xamarin.Forms.Controls/CoreGallery.cs | 21 ++- Xamarin.Forms.Core/TabbedPage.cs | 28 ++++ .../AppCompat/TabbedPageRenderer.cs | 54 ++++++- Xamarin.Forms.Platform.UAP/Resources.xaml | 8 +- Xamarin.Forms.Platform.UAP/TabbedPageRenderer.cs | 45 +++++- .../PhoneResources.xaml | 4 +- .../TabbedPageRenderer.cs | 163 +++++++++++++-------- .../TabbedPageRenderer.cs | 35 ++++- .../TabletResources.xaml | 4 +- Xamarin.Forms.Platform.WinRT.Tablet/TabsControl.cs | 16 ++ .../Renderers/NavigationRenderer.cs | 4 +- .../Renderers/TabbedRenderer.cs | 55 ++++++- .../Xamarin.Forms/TabbedPage.xml | 63 +++++++- 13 files changed, 413 insertions(+), 87 deletions(-) diff --git a/Xamarin.Forms.Controls/CoreGallery.cs b/Xamarin.Forms.Controls/CoreGallery.cs index 21c45e08..7b6ed150 100644 --- a/Xamarin.Forms.Controls/CoreGallery.cs +++ b/Xamarin.Forms.Controls/CoreGallery.cs @@ -78,40 +78,45 @@ namespace Xamarin.Forms.Controls { protected override void Init () { - } #if APP public CoreTabbedPage () { AutomationId = "TabbedPageRoot"; - Children.Add (new CoreRootPage (this, NavigationBehavior.PushModalAsync) { Title = "Tab 1" }); - Children.Add (new CoreRootPage (this, NavigationBehavior.PushModalAsync) { Title = "Tab 2" }); + + BarBackgroundColor = Color.Maroon; + BarTextColor = Color.White; + + Children.Add(new CoreRootPage(this, NavigationBehavior.PushModalAsync) { Title = "Tab 1" }); + Children.Add(new CoreRootPage(this, NavigationBehavior.PushModalAsync) { Title = "Tab 2" }); Children.Add(new NavigationPage(new Page()) { Title = "Rubriques", + Icon = "coffee.png", + BarBackgroundColor = Color.Blue }); Children.Add(new NavigationPage(new Page()) { - Title = "Le Club", + Title = "Le Club" }); - Children.Add(new NavigationPage(new Page{Title = "Bookmarks"}) + Children.Add(new NavigationPage(new Page { Title = "Bookmarks" }) { Title = "Bookmarks", }); - Children.Add(new NavigationPage(new Page() { Title = "Alertes" }) + Children.Add(new NavigationPage(new Page { Title = "Alertes" }) { Title = "Notifications", }); - Children.Add(new NavigationPage (new Page(){Title = "My account"}) + Children.Add(new NavigationPage(new Page { Title = "My account" }) { Title = "My account", }); - Children.Add(new NavigationPage(new Page(){Title = "About"}) + Children.Add(new NavigationPage(new Page { Title = "About" }) { Title = "About", }); diff --git a/Xamarin.Forms.Core/TabbedPage.cs b/Xamarin.Forms.Core/TabbedPage.cs index 53b736cb..5fb6bf9c 100644 --- a/Xamarin.Forms.Core/TabbedPage.cs +++ b/Xamarin.Forms.Core/TabbedPage.cs @@ -5,6 +5,34 @@ namespace Xamarin.Forms [RenderWith(typeof(_TabbedPageRenderer))] public class TabbedPage : MultiPage { + public static readonly BindableProperty BarBackgroundColorProperty = BindableProperty.Create(nameof(BarBackgroundColor), typeof(Color), typeof(TabbedPage), Color.Default); + + public static readonly BindableProperty BarTextColorProperty = BindableProperty.Create(nameof(BarTextColor), typeof(Color), typeof(TabbedPage), Color.Default); + + public Color BarBackgroundColor + { + get + { + return (Color)GetValue(BarBackgroundColorProperty); + } + set + { + SetValue(BarBackgroundColorProperty, value); + } + } + + public Color BarTextColor + { + get + { + return (Color)GetValue(BarTextColorProperty); + } + set + { + SetValue(BarTextColorProperty, value); + } + } + protected override Page CreateDefault(object item) { var page = new Page(); diff --git a/Xamarin.Forms.Platform.Android/AppCompat/TabbedPageRenderer.cs b/Xamarin.Forms.Platform.Android/AppCompat/TabbedPageRenderer.cs index f978028b..28fbf49c 100644 --- a/Xamarin.Forms.Platform.Android/AppCompat/TabbedPageRenderer.cs +++ b/Xamarin.Forms.Platform.Android/AppCompat/TabbedPageRenderer.cs @@ -2,6 +2,9 @@ using System; using System.Collections.Specialized; using System.ComponentModel; using Android.Content; +using Android.Content.Res; +using Android.Graphics; +using Android.Graphics.Drawables; using Android.OS; using Android.Runtime; using Android.Support.Design.Widget; @@ -13,6 +16,7 @@ namespace Xamarin.Forms.Platform.Android.AppCompat { public class TabbedPageRenderer : VisualElementRenderer, TabLayout.IOnTabSelectedListener, ViewPager.IOnPageChangeListener, IManageFragments { + Drawable _backgroundDrawable; bool _disposed; FragmentManager _fragmentManager; TabLayout _tabLayout; @@ -149,7 +153,7 @@ namespace Xamarin.Forms.Platform.Android.AppCompat tabs = _tabLayout = new TabLayout(activity) { TabMode = TabLayout.ModeFixed, TabGravity = TabLayout.GravityFill }; FormsViewPager pager = _viewPager = - new FormsViewPager(activity) + new FormsViewPager(activity) { OverScrollMode = OverScrollMode.Never, EnableGesture = UseAnimations, @@ -173,6 +177,8 @@ namespace Xamarin.Forms.Platform.Android.AppCompat UpdateIgnoreContainerAreas(); tabbedPage.InternalChildren.CollectionChanged += OnChildrenCollectionChanged; + UpdateBarBackgroundColor(); + UpdateBarTextColor(); } } @@ -180,8 +186,12 @@ namespace Xamarin.Forms.Platform.Android.AppCompat { base.OnElementPropertyChanged(sender, e); - if (e.PropertyName == "CurrentPage") + if (e.PropertyName == nameof(TabbedPage.CurrentPage)) ScrollToCurrentPage(); + else if (e.PropertyName == NavigationPage.BarBackgroundColorProperty.PropertyName) + UpdateBarBackgroundColor(); + else if (e.PropertyName == NavigationPage.BarTextColorProperty.PropertyName) + UpdateBarTextColor(); } protected override void OnLayout(bool changed, int l, int t, int r, int b) @@ -318,5 +328,45 @@ namespace Xamarin.Forms.Platform.Android.AppCompat tab.SetIcon(ResourceManager.IdFromTitle(icon, ResourceManager.DrawableClass)); } } + + void UpdateBarBackgroundColor() + { + if (_disposed || _tabLayout == null) + return; + + Color tintColor = Element.BarBackgroundColor; + + if (Forms.IsLollipopOrNewer) + { + if (tintColor.IsDefault) + _tabLayout.BackgroundTintMode = null; + else + { + _tabLayout.BackgroundTintMode = PorterDuff.Mode.Src; + _tabLayout.BackgroundTintList = ColorStateList.ValueOf(tintColor.ToAndroid()); + } + } + else + { + if (tintColor.IsDefault && _backgroundDrawable != null) + _tabLayout.SetBackground(_backgroundDrawable); + else if (!tintColor.IsDefault) + { + if (_backgroundDrawable == null) + _backgroundDrawable = _tabLayout.Background; + _tabLayout.SetBackgroundColor(tintColor.ToAndroid()); + } + } + } + + private void UpdateBarTextColor() + { + if (_disposed || _tabLayout == null) + return; + + var textColor = Element.BarTextColor.ToAndroid().ToArgb(); + + _tabLayout.SetTabTextColors(textColor, textColor); + } } } \ No newline at end of file diff --git a/Xamarin.Forms.Platform.UAP/Resources.xaml b/Xamarin.Forms.Platform.UAP/Resources.xaml index bca87974..23753c13 100644 --- a/Xamarin.Forms.Platform.UAP/Resources.xaml +++ b/Xamarin.Forms.Platform.UAP/Resources.xaml @@ -238,7 +238,9 @@ - + + + @@ -471,7 +473,7 @@ - + @@ -616,7 +618,7 @@ - + diff --git a/Xamarin.Forms.Platform.UAP/TabbedPageRenderer.cs b/Xamarin.Forms.Platform.UAP/TabbedPageRenderer.cs index 1f1e15fc..cde40717 100644 --- a/Xamarin.Forms.Platform.UAP/TabbedPageRenderer.cs +++ b/Xamarin.Forms.Platform.UAP/TabbedPageRenderer.cs @@ -15,7 +15,7 @@ namespace Xamarin.Forms.Platform.UWP { Loaded += TabbedPagePresenter_Loaded; Unloaded += TabbedPagePresenter_Unloaded; - SizeChanged += (s, e) => + SizeChanged += (s, e) => { if (ActualWidth > 0 && ActualHeight > 0) { @@ -44,7 +44,7 @@ namespace Xamarin.Forms.Platform.UWP bool _showTitle; VisualElementTracker _tracker; - public Pivot Control { get; private set; } + public FormsPivot Control { get; private set; } public TabbedPage Element { get; private set; } @@ -157,7 +157,7 @@ namespace Xamarin.Forms.Platform.UWP { if (Control == null) { - Control = new FormsPivot { Style = (Windows.UI.Xaml.Style)Windows.UI.Xaml.Application.Current.Resources["TabbedPageStyle"] }; + Control = new FormsPivot { Style = (Windows.UI.Xaml.Style)Windows.UI.Xaml.Application.Current.Resources["TabbedPageStyle"], }; Control.SelectionChanged += OnSelectionChanged; @@ -170,6 +170,8 @@ namespace Xamarin.Forms.Platform.UWP Control.DataContext = Element; OnPagesChanged(Element.Children, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); UpdateCurrentPage(); + UpdateBarTextColor(); + UpdateBarBackgroundColor(); ((INotifyCollectionChanged)Element.Children).CollectionChanged += OnPagesChanged; element.PropertyChanged += OnElementPropertyChanged; @@ -178,6 +180,7 @@ namespace Xamarin.Forms.Platform.UWP Control.SetValue(AutomationProperties.AutomationIdProperty, element.AutomationId); } + OnElementChanged(new VisualElementChangedEventArgs(oldElement, element)); } @@ -201,8 +204,13 @@ namespace Xamarin.Forms.Platform.UWP void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { - if (e.PropertyName == "CurrentPage") + if (e.PropertyName == nameof(TabbedPage.CurrentPage)) UpdateCurrentPage(); + else if (e.PropertyName == TabbedPage.BarTextColorProperty.PropertyName) + UpdateBarTextColor(); + else if (e.PropertyName == TabbedPage.BarBackgroundColorProperty.PropertyName) + UpdateBarBackgroundColor(); + } void OnLoaded(object sender, RoutedEventArgs args) @@ -243,6 +251,32 @@ namespace Xamarin.Forms.Platform.UWP Element.SendDisappearing(); } + Brush GetBarBackgroundBrush() + { + object defaultColor = Windows.UI.Xaml.Application.Current.Resources["SystemControlBackgroundChromeMediumLowBrush"]; + if (Element.BarBackgroundColor.IsDefault && defaultColor != null) + return (Brush)defaultColor; + return Element.BarBackgroundColor.ToBrush(); + } + + Brush GetBarForegroundBrush() + { + object defaultColor = Windows.UI.Xaml.Application.Current.Resources["ApplicationForegroundThemeBrush"]; + if (Element.BarTextColor.IsDefault) + return (Brush)defaultColor; + return Element.BarTextColor.ToBrush(); + } + + void UpdateBarBackgroundColor() + { + Control.ToolbarBackground = GetBarBackgroundBrush(); + } + + void UpdateBarTextColor() + { + Control.ToolbarForeground = GetBarForegroundBrush(); + } + void UpdateBarVisibility() { (Control as FormsPivot).ToolbarVisibility = _showTitle ? Visibility.Visible : Visibility.Collapsed; @@ -255,6 +289,9 @@ namespace Xamarin.Forms.Platform.UWP var nav = page as NavigationPage; ((ITitleProvider)this).ShowTitle = nav != null; + UpdateBarTextColor(); + UpdateBarBackgroundColor(); + if (page == null) return; diff --git a/Xamarin.Forms.Platform.WinRT.Phone/PhoneResources.xaml b/Xamarin.Forms.Platform.WinRT.Phone/PhoneResources.xaml index 61f6667e..4dcf4be5 100644 --- a/Xamarin.Forms.Platform.WinRT.Phone/PhoneResources.xaml +++ b/Xamarin.Forms.Platform.WinRT.Phone/PhoneResources.xaml @@ -242,7 +242,7 @@ - + @@ -281,7 +281,7 @@ - + diff --git a/Xamarin.Forms.Platform.WinRT.Phone/TabbedPageRenderer.cs b/Xamarin.Forms.Platform.WinRT.Phone/TabbedPageRenderer.cs index 9de3a623..a7337301 100644 --- a/Xamarin.Forms.Platform.WinRT.Phone/TabbedPageRenderer.cs +++ b/Xamarin.Forms.Platform.WinRT.Phone/TabbedPageRenderer.cs @@ -8,14 +8,15 @@ using Windows.UI.Xaml.Media; namespace Xamarin.Forms.Platform.WinRT { - internal class TabbedPagePresenter : Windows.UI.Xaml.Controls.ContentPresenter + internal class TabbedPagePresenter : Windows.UI.Xaml.Controls.ContentPresenter { - public TabbedPagePresenter () + public TabbedPagePresenter() { SizeChanged += (s, e) => { - if (ActualWidth > 0 && ActualHeight > 0) { - var tab = ((Page) DataContext); - ((TabbedPage) tab.RealParent).ContainerArea = new Rectangle (0, 0, ActualWidth, ActualHeight); + if (ActualWidth > 0 && ActualHeight > 0) + { + var tab = ((Page)DataContext); + ((TabbedPage)tab.RealParent).ContainerArea = new Rectangle(0, 0, ActualWidth, ActualHeight); } }; } @@ -36,7 +37,7 @@ namespace Xamarin.Forms.Platform.WinRT get { return Element; } } - public Pivot Control + public FormsPivot Control { get; private set; @@ -48,31 +49,34 @@ namespace Xamarin.Forms.Platform.WinRT private set; } - public void SetElement (VisualElement element) + public void SetElement(VisualElement element) { if (element != null && !(element is TabbedPage)) - throw new ArgumentException ("Element must be a TabbedPage", "element"); + throw new ArgumentException("Element must be a TabbedPage", "element"); TabbedPage oldElement = Element; - Element = (TabbedPage) element; + Element = (TabbedPage)element; - if (oldElement != null) { + if (oldElement != null) + { oldElement.PropertyChanged -= OnElementPropertyChanged; - ((INotifyCollectionChanged) oldElement.Children).CollectionChanged -= OnPagesChanged; + ((INotifyCollectionChanged)oldElement.Children).CollectionChanged -= OnPagesChanged; } - if (element != null) { - if (Control == null) { + if (element != null) + { + if (Control == null) + { Control = new FormsPivot { - Style = (Windows.UI.Xaml.Style) Windows.UI.Xaml.Application.Current.Resources["TabbedPageStyle"] + Style = (Windows.UI.Xaml.Style)Windows.UI.Xaml.Application.Current.Resources["TabbedPageStyle"] }; Control.HeaderTemplate = (Windows.UI.Xaml.DataTemplate)Windows.UI.Xaml.Application.Current.Resources["TabbedPageHeader"]; Control.ItemTemplate = (Windows.UI.Xaml.DataTemplate)Windows.UI.Xaml.Application.Current.Resources["TabbedPage"]; Control.SelectionChanged += OnSelectionChanged; - Tracker = new BackgroundTracker (Windows.UI.Xaml.Controls.Control.BackgroundProperty) { - Element = (Page) element, + Tracker = new BackgroundTracker(Windows.UI.Xaml.Controls.Control.BackgroundProperty) { + Element = (Page)element, Control = Control, Container = Control }; @@ -82,22 +86,24 @@ namespace Xamarin.Forms.Platform.WinRT } Control.DataContext = Element; - OnPagesChanged (Element.Children, new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Reset)); - UpdateCurrentPage (); - - ((INotifyCollectionChanged) Element.Children).CollectionChanged += OnPagesChanged; + OnPagesChanged(Element.Children, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); + UpdateCurrentPage(); + UpdateBarTextColor(); + UpdateBarBackgroundColor(); + + ((INotifyCollectionChanged)Element.Children).CollectionChanged += OnPagesChanged; element.PropertyChanged += OnElementPropertyChanged; - if (!string.IsNullOrEmpty (element.AutomationId)) - Control.SetValue (AutomationProperties.AutomationIdProperty, element.AutomationId); + if (!string.IsNullOrEmpty(element.AutomationId)) + Control.SetValue(AutomationProperties.AutomationIdProperty, element.AutomationId); } - OnElementChanged (new VisualElementChangedEventArgs (oldElement, element)); + OnElementChanged(new VisualElementChangedEventArgs(oldElement, element)); } - public SizeRequest GetDesiredSize (double widthConstraint, double heightConstraint) + public SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint) { - var constraint = new Windows.Foundation.Size (widthConstraint, heightConstraint); + var constraint = new Windows.Foundation.Size(widthConstraint, heightConstraint); var oldWidth = Control.Width; var oldHeight = Control.Height; @@ -105,27 +111,27 @@ namespace Xamarin.Forms.Platform.WinRT Control.Height = double.NaN; Control.Width = double.NaN; - Control.Measure (constraint); - var result = new Size (Math.Ceiling (Control.DesiredSize.Width), Math.Ceiling (Control.DesiredSize.Height)); + Control.Measure(constraint); + var result = new Size(Math.Ceiling(Control.DesiredSize.Width), Math.Ceiling(Control.DesiredSize.Height)); Control.Width = oldWidth; Control.Height = oldHeight; - return new SizeRequest (result); + return new SizeRequest(result); } - public void Dispose () + public void Dispose() { - Dispose (true); + Dispose(true); } - protected virtual void Dispose (bool disposing) + protected virtual void Dispose(bool disposing) { if (!disposing || _disposed) return; _disposed = true; - SetElement (null); + SetElement(null); Tracker = null; } @@ -140,8 +146,9 @@ namespace Xamarin.Forms.Platform.WinRT if (_tracker == value) return; - if (_tracker != null) { - _tracker.Dispose (); + if (_tracker != null) + { + _tracker.Dispose(); /*this.tracker.Updated -= OnTrackerUpdated;*/ } @@ -151,8 +158,8 @@ namespace Xamarin.Forms.Platform.WinRT this.tracker.Updated += OnTrackerUpdated;*/ } } - - bool ITitleProvider.ShowTitle + + bool ITitleProvider.ShowTitle { get { @@ -164,12 +171,12 @@ namespace Xamarin.Forms.Platform.WinRT if (_showTitle == value) return; _showTitle = value; - + (Control as FormsPivot).ToolbarVisibility = _showTitle ? Visibility.Visible : Visibility.Collapsed; } } - string ITitleProvider.Title + string ITitleProvider.Title { get { @@ -199,86 +206,118 @@ namespace Xamarin.Forms.Platform.WinRT } } - protected virtual void OnElementChanged (VisualElementChangedEventArgs e) + protected virtual void OnElementChanged(VisualElementChangedEventArgs e) { var changed = ElementChanged; if (changed != null) - changed (this, e); + changed(this, e); } bool _disposed; VisualElementTracker _tracker; bool _showTitle; - void OnPagesChanged (object sender, NotifyCollectionChangedEventArgs e) + void OnPagesChanged(object sender, NotifyCollectionChangedEventArgs e) { - e.Apply (Element.Children, Control.Items); + e.Apply(Element.Children, Control.Items); // Potential performance issue, UpdateLayout () is called for every page change - Control.UpdateLayout (); + Control.UpdateLayout(); } - void OnSelectionChanged (object sender, SelectionChangedEventArgs e) + void OnSelectionChanged(object sender, SelectionChangedEventArgs e) { if (Element == null) return; - Page page = (e.AddedItems.Count > 0) ? (Page) e.AddedItems[0] : null; + Page page = (e.AddedItems.Count > 0) ? (Page)e.AddedItems[0] : null; Element.CurrentPage = page; } - void OnElementPropertyChanged (object sender, PropertyChangedEventArgs e) + void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { - if (e.PropertyName == "CurrentPage") - UpdateCurrentPage (); + if (e.PropertyName == nameof(TabbedPage.CurrentPage)) + UpdateCurrentPage(); + else if (e.PropertyName == TabbedPage.BarTextColorProperty.PropertyName) + UpdateBarTextColor(); + else if (e.PropertyName == TabbedPage.BarBackgroundColorProperty.PropertyName) + UpdateBarBackgroundColor(); } - void UpdateCurrentPage () + void UpdateCurrentPage() { Page page = Element.CurrentPage; - UpdateTitle (page); + UpdateTitle(page); if (page == null) return; Control.SelectedItem = page; } - void OnLoaded (object sender, RoutedEventArgs args) + void OnLoaded(object sender, RoutedEventArgs args) { if (Element == null) return; - Element.SendAppearing (); + Element.SendAppearing(); } - void OnUnloaded (object sender, RoutedEventArgs args) + void OnUnloaded(object sender, RoutedEventArgs args) { if (Element == null) return; - Element.SendDisappearing (); + Element.SendDisappearing(); + } + + void OnTrackerUpdated(object sender, EventArgs e) + { + + } + + Brush GetBarBackgroundBrush() + { + object defaultColor = Windows.UI.Xaml.Application.Current.Resources["AppBarBackgroundThemeBrush"]; + if (Element.BarBackgroundColor.IsDefault && defaultColor != null) + return (Brush)defaultColor; + return Element.BarBackgroundColor.ToBrush(); + } + + Brush GetBarForegroundBrush() + { + object defaultColor = Windows.UI.Xaml.Application.Current.Resources["AppBarItemForegroundThemeBrush"]; + if (Element.BarTextColor.IsDefault) + return (Brush)defaultColor; + return Element.BarTextColor.ToBrush(); } - void OnTrackerUpdated (object sender, EventArgs e) + void UpdateBarBackgroundColor() { + Control.ToolbarBackground = GetBarBackgroundBrush(); + } + void UpdateBarTextColor() + { + Control.ToolbarForeground = GetBarForegroundBrush(); } - void UpdateTitle (Page child) + void UpdateTitle(Page child) { - Control.ClearValue (Pivot.TitleProperty); + Control.ClearValue(Pivot.TitleProperty); if (child == null) return; - var renderer = Platform.GetRenderer (child); + var renderer = Platform.GetRenderer(child); var navigationRenderer = renderer as NavigationPageRenderer; - if (navigationRenderer != null) { + if (navigationRenderer != null) + { Control.Title = navigationRenderer.Title; - } else { - ((ITitleProvider) this).ShowTitle = false; + } + else { + ((ITitleProvider)this).ShowTitle = false; } } - + } } \ No newline at end of file diff --git a/Xamarin.Forms.Platform.WinRT.Tablet/TabbedPageRenderer.cs b/Xamarin.Forms.Platform.WinRT.Tablet/TabbedPageRenderer.cs index 33923a49..f1df0813 100644 --- a/Xamarin.Forms.Platform.WinRT.Tablet/TabbedPageRenderer.cs +++ b/Xamarin.Forms.Platform.WinRT.Tablet/TabbedPageRenderer.cs @@ -3,6 +3,7 @@ using System.Collections.Specialized; using System.ComponentModel; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; +using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Media.Animation; namespace Xamarin.Forms.Platform.WinRT @@ -87,6 +88,8 @@ namespace Xamarin.Forms.Platform.WinRT OnPagesChanged(Page.Children, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); UpdateCurrentPage(); + UpdateBarTextColor(); + UpdateBarBackgroundColor(); ((INotifyCollectionChanged)Page.Children).CollectionChanged += OnPagesChanged; element.PropertyChanged += OnElementPropertyChanged; @@ -95,6 +98,22 @@ namespace Xamarin.Forms.Platform.WinRT OnElementChanged(new VisualElementChangedEventArgs(oldElement, element)); } + Brush GetBarBackgroundBrush() + { + object defaultColor = Windows.UI.Xaml.Application.Current.Resources["ApplicationPageBackgroundThemeBrush"]; + if (Page.BarBackgroundColor.IsDefault && defaultColor != null) + return (Brush)defaultColor; + return Page.BarBackgroundColor.ToBrush(); + } + + Brush GetBarForegroundBrush() + { + object defaultColor = Windows.UI.Xaml.Application.Current.Resources["ApplicationForegroundThemeBrush"]; + if (Page.BarTextColor.IsDefault) + return (Brush)defaultColor; + return Page.BarTextColor.ToBrush(); + } + public SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint) { if (_canvas.Children.Count == 0) @@ -153,8 +172,22 @@ namespace Xamarin.Forms.Platform.WinRT void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { - if (e.PropertyName == "CurrentPage") + if (e.PropertyName == nameof(TabbedPage.CurrentPage)) UpdateCurrentPage(); + else if (e.PropertyName == TabbedPage.BarTextColorProperty.PropertyName) + UpdateBarTextColor(); + else if (e.PropertyName == TabbedPage.BarBackgroundColorProperty.PropertyName) + UpdateBarBackgroundColor(); + } + + void UpdateBarBackgroundColor() + { + _tabs.Background = GetBarBackgroundBrush(); + } + + void UpdateBarTextColor() + { + _tabs.Foreground = GetBarForegroundBrush(); } void UpdateCurrentPage() diff --git a/Xamarin.Forms.Platform.WinRT.Tablet/TabletResources.xaml b/Xamarin.Forms.Platform.WinRT.Tablet/TabletResources.xaml index 51b9e1d7..cfd51257 100644 --- a/Xamarin.Forms.Platform.WinRT.Tablet/TabletResources.xaml +++ b/Xamarin.Forms.Platform.WinRT.Tablet/TabletResources.xaml @@ -250,7 +250,7 @@ - + @@ -261,7 +261,7 @@ + Style="{ThemeResource CaptionTextBlockStyle}" FontWeight="SemiBold" Foreground="{TemplateBinding ToolbarForeground}" HorizontalAlignment="Left" /> diff --git a/Xamarin.Forms.Platform.WinRT.Tablet/TabsControl.cs b/Xamarin.Forms.Platform.WinRT.Tablet/TabsControl.cs index d98a27a0..0848772c 100644 --- a/Xamarin.Forms.Platform.WinRT.Tablet/TabsControl.cs +++ b/Xamarin.Forms.Platform.WinRT.Tablet/TabsControl.cs @@ -1,6 +1,7 @@ using System; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; +using Windows.UI.Xaml.Media; namespace Xamarin.Forms.Platform.WinRT { @@ -46,5 +47,20 @@ namespace Xamarin.Forms.Platform.WinRT public class TabsControl : ItemsControl { + + public static readonly DependencyProperty ToolbarForegroundProperty = DependencyProperty.Register(nameof(ToolbarForeground), typeof(Brush), typeof(TabsControl), new PropertyMetadata(default(Brush))); + public static readonly DependencyProperty ToolbarBackgroundProperty = DependencyProperty.Register(nameof(ToolbarBackground), typeof(Brush), typeof(TabsControl), new PropertyMetadata(default(Brush))); + + public Brush ToolbarBackground + { + get { return (Brush)GetValue(ToolbarBackgroundProperty); } + set { SetValue(ToolbarBackgroundProperty, value); } + } + + public Brush ToolbarForeground + { + get { return (Brush)GetValue(ToolbarForegroundProperty); } + set { SetValue(ToolbarForegroundProperty, value); } + } } } \ No newline at end of file diff --git a/Xamarin.Forms.Platform.iOS/Renderers/NavigationRenderer.cs b/Xamarin.Forms.Platform.iOS/Renderers/NavigationRenderer.cs index f09fec9a..da3a0aa2 100644 --- a/Xamarin.Forms.Platform.iOS/Renderers/NavigationRenderer.cs +++ b/Xamarin.Forms.Platform.iOS/Renderers/NavigationRenderer.cs @@ -594,6 +594,8 @@ namespace Xamarin.Forms.Platform.iOS { var titleAttributes = new UIStringAttributes(); titleAttributes.Font = globalAttributes.Font; + // TODO: the ternary if statement here will always return false because of the encapsulating if statement. + // What was the intention? titleAttributes.ForegroundColor = barTextColor == Color.Default ? titleAttributes.ForegroundColor ?? UINavigationBar.Appearance.TintColor : barTextColor.ToUIColor(); @@ -640,7 +642,7 @@ namespace Xamarin.Forms.Platform.iOS { containerController.NavigationItem.LeftBarButtonItem = new UIBarButtonItem(new UIImage(_parentMasterDetailPage.Master.Icon), UIBarButtonItemStyle.Plain, - (o, e) => _parentMasterDetailPage.IsPresented = !_parentMasterDetailPage.IsPresented); + (o, e) => _parentMasterDetailPage.IsPresented = !_parentMasterDetailPage.IsPresented); } catch (Exception) { diff --git a/Xamarin.Forms.Platform.iOS/Renderers/TabbedRenderer.cs b/Xamarin.Forms.Platform.iOS/Renderers/TabbedRenderer.cs index 14bf4a24..2db8ac26 100644 --- a/Xamarin.Forms.Platform.iOS/Renderers/TabbedRenderer.cs +++ b/Xamarin.Forms.Platform.iOS/Renderers/TabbedRenderer.cs @@ -74,6 +74,9 @@ namespace Xamarin.Forms.Platform.iOS //disable edit/reorder of tabs CustomizableViewControllers = null; + UpdateBarBackgroundColor(); + UpdateBarTextColor(); + EffectUtilities.RegisterEffectControlProvider(this, oldElement, element); } @@ -227,7 +230,7 @@ namespace Xamarin.Forms.Platform.iOS void OnPropertyChanged(object sender, PropertyChangedEventArgs e) { - if (e.PropertyName == "CurrentPage") + if (e.PropertyName == nameof(TabbedPage.CurrentPage)) { var current = Tabbed.CurrentPage; if (current == null) @@ -239,6 +242,10 @@ namespace Xamarin.Forms.Platform.iOS SelectedViewController = controller; } + else if (e.PropertyName == TabbedPage.BarBackgroundColorProperty.PropertyName) + UpdateBarBackgroundColor(); + else if (e.PropertyName == TabbedPage.BarTextColorProperty.PropertyName) + UpdateBarTextColor(); } void Reset() @@ -292,6 +299,52 @@ namespace Xamarin.Forms.Platform.iOS Platform.SetRenderer(page, null); } + void UpdateBarBackgroundColor() + { + if (Tabbed == null || TabBar == null) + return; + + var barBackgroundColor = Tabbed.BarBackgroundColor; + + if (Forms.IsiOS7OrNewer) + { + TabBar.BarTintColor = barBackgroundColor == Color.Default ? UINavigationBar.Appearance.BarTintColor : barBackgroundColor.ToUIColor(); + } + else + { + TabBar.TintColor = barBackgroundColor == Color.Default ? UINavigationBar.Appearance.TintColor : barBackgroundColor.ToUIColor(); + } + } + + void UpdateBarTextColor() + { + if (Tabbed == null || TabBar == null || TabBar.Items == null) + return; + + var barTextColor = Tabbed.BarTextColor; + + var globalAttributes = UINavigationBar.Appearance.GetTitleTextAttributes(); + + var attributes = new UITextAttributes { Font = globalAttributes.Font }; + + if (barTextColor == Color.Default) + attributes.TextColor = globalAttributes.TextColor; + else + attributes.TextColor = barTextColor.ToUIColor(); + + foreach (UITabBarItem item in TabBar.Items) + { + item.SetTitleTextAttributes(attributes, UIControlState.Normal); + } + + // set TintColor for selected icon + // setting the unselected icon tint is not supported by iOS + if (Forms.IsiOS7OrNewer) + { + TabBar.TintColor = barTextColor == Color.Default ? UINavigationBar.Appearance.TintColor : barTextColor.ToUIColor(); + } + } + void UpdateChildrenOrderIndex(UIViewController[] viewControllers) { for (var i = 0; i < viewControllers.Length; i++) diff --git a/docs/Xamarin.Forms.Core/Xamarin.Forms/TabbedPage.xml b/docs/Xamarin.Forms.Core/Xamarin.Forms/TabbedPage.xml index aee2d899..130750fc 100644 --- a/docs/Xamarin.Forms.Core/Xamarin.Forms/TabbedPage.xml +++ b/docs/Xamarin.Forms.Core/Xamarin.Forms/TabbedPage.xml @@ -159,6 +159,68 @@ class TabbedPageDemoPage2 : TabbedPage To be added. + + + + Property + + 2.0.0.0 + + + Xamarin.Forms.Color + + + To be added. + To be added. + To be added. + + + + + + Field + + 2.0.0.0 + + + Xamarin.Forms.BindableProperty + + + To be added. + To be added. + + + + + + Property + + 2.0.0.0 + + + Xamarin.Forms.Color + + + To be added. + To be added. + To be added. + + + + + + Field + + 2.0.0.0 + + + Xamarin.Forms.BindableProperty + + + To be added. + To be added. + + @@ -196,7 +258,6 @@ class TabbedPageDemoPage2 : TabbedPage 1.3.0.0 1.4.0.0 1.5.0.0 - 2.0.0.0 System.Void -- cgit v1.2.3