From a490740a2e76b9c53735d9778d4c6e0fa9e4bf22 Mon Sep 17 00:00:00 2001 From: Samantha Houts Date: Mon, 18 Jul 2016 16:33:41 -0700 Subject: [Win] Setting TabbedPage.BarTextColor works (#244) [Win] TabbedPage BarBG takes precedence --- Xamarin.Forms.Platform.UAP/Resources.xaml | 6 +- Xamarin.Forms.Platform.UAP/TabbedPageRenderer.cs | 73 +++++++++++++++++++----- 2 files changed, 61 insertions(+), 18 deletions(-) (limited to 'Xamarin.Forms.Platform.UAP') diff --git a/Xamarin.Forms.Platform.UAP/Resources.xaml b/Xamarin.Forms.Platform.UAP/Resources.xaml index 7a11fe4f..5df785f0 100644 --- a/Xamarin.Forms.Platform.UAP/Resources.xaml +++ b/Xamarin.Forms.Platform.UAP/Resources.xaml @@ -463,7 +463,7 @@ - + @@ -473,7 +473,7 @@ - + @@ -618,7 +618,7 @@ - + diff --git a/Xamarin.Forms.Platform.UAP/TabbedPageRenderer.cs b/Xamarin.Forms.Platform.UAP/TabbedPageRenderer.cs index 0e6ce0b4..aa988b80 100644 --- a/Xamarin.Forms.Platform.UAP/TabbedPageRenderer.cs +++ b/Xamarin.Forms.Platform.UAP/TabbedPageRenderer.cs @@ -7,6 +7,7 @@ using Windows.UI.Xaml.Automation; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media; using Xamarin.Forms.Internals; +using WGrid = Windows.UI.Xaml.Controls.Grid; namespace Xamarin.Forms.Platform.UWP { @@ -16,7 +17,7 @@ namespace Xamarin.Forms.Platform.UWP { Loaded += TabbedPagePresenter_Loaded; Unloaded += TabbedPagePresenter_Unloaded; - SizeChanged += (s, e) => + SizeChanged += (s, e) => { if (ActualWidth > 0 && ActualHeight > 0) { @@ -41,10 +42,17 @@ namespace Xamarin.Forms.Platform.UWP public class TabbedPageRenderer : IVisualElementRenderer, ITitleProvider, IToolbarProvider { + const string TabBarHeaderTextBlockName = "TabbedPageHeaderTextBlock"; + const string TabBarHeaderGridName = "TabbedPageHeaderGrid"; + + Color _barBackgroundColor; + Color _barTextColor; bool _disposed; bool _showTitle; VisualElementTracker _tracker; + ITitleProvider TitleProvider => this; + public FormsPivot Control { get; private set; } public TabbedPage Element { get; private set; } @@ -71,12 +79,12 @@ namespace Xamarin.Forms.Platform.UWP Brush ITitleProvider.BarBackgroundBrush { - set { (Control as FormsPivot).ToolbarBackground = value; } + set { Control.ToolbarBackground = value; } } Brush ITitleProvider.BarForegroundBrush { - set { (Control as FormsPivot).ToolbarForeground = value; } + set { Control.ToolbarForeground = value; } } IPageController PageController => Element as IPageController; @@ -172,9 +180,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; @@ -183,7 +190,6 @@ namespace Xamarin.Forms.Platform.UWP Control.SetValue(AutomationProperties.AutomationIdProperty, element.AutomationId); } - OnElementChanged(new VisualElementChangedEventArgs(oldElement, element)); } @@ -208,17 +214,22 @@ namespace Xamarin.Forms.Platform.UWP void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == nameof(TabbedPage.CurrentPage)) + { UpdateCurrentPage(); + UpdateBarTextColor(); + UpdateBarBackgroundColor(); + } else if (e.PropertyName == TabbedPage.BarTextColorProperty.PropertyName) UpdateBarTextColor(); else if (e.PropertyName == TabbedPage.BarBackgroundColorProperty.PropertyName) UpdateBarBackgroundColor(); - } void OnLoaded(object sender, RoutedEventArgs args) { PageController?.SendAppearing(); + UpdateBarTextColor(); + UpdateBarBackgroundColor(); } void OnPagesChanged(object sender, NotifyCollectionChangedEventArgs e) @@ -259,24 +270,59 @@ namespace Xamarin.Forms.Platform.UWP Brush GetBarForegroundBrush() { object defaultColor = Windows.UI.Xaml.Application.Current.Resources["ApplicationForegroundThemeBrush"]; - if (Element.BarTextColor.IsDefault) + if (Element.BarTextColor.IsDefault && defaultColor != null) return (Brush)defaultColor; return Element.BarTextColor.ToBrush(); } void UpdateBarBackgroundColor() { - Control.ToolbarBackground = GetBarBackgroundBrush(); + if (Element == null) return; + var barBackgroundColor = Element.BarBackgroundColor; + + if (barBackgroundColor == _barBackgroundColor) return; + _barBackgroundColor = barBackgroundColor; + + var controlToolbarBackground = Control.ToolbarBackground; + if (controlToolbarBackground == null && barBackgroundColor.IsDefault) return; + + var brush = GetBarBackgroundBrush(); + if (brush == controlToolbarBackground) return; + + TitleProvider.BarBackgroundBrush = brush; + + foreach (WGrid tabBarGrid in Control.GetDescendantsByName(TabBarHeaderGridName)) + { + tabBarGrid.Background = brush; + } } void UpdateBarTextColor() { - Control.ToolbarForeground = GetBarForegroundBrush(); + if (Element == null) return; + var barTextColor = Element.BarTextColor; + + if (barTextColor == _barTextColor) return; + _barTextColor = barTextColor; + + var controlToolbarForeground = Control.ToolbarForeground; + if (controlToolbarForeground == null && barTextColor.IsDefault) return; + + var brush = GetBarForegroundBrush(); + if (brush == controlToolbarForeground) + return; + + TitleProvider.BarForegroundBrush = brush; + + foreach (TextBlock tabBarTextBlock in Control.GetDescendantsByName(TabBarHeaderTextBlockName)) + { + tabBarTextBlock.Foreground = brush; + } } void UpdateBarVisibility() { - (Control as FormsPivot).ToolbarVisibility = _showTitle ? Visibility.Visible : Visibility.Collapsed; + Control.ToolbarVisibility = _showTitle ? Visibility.Visible : Visibility.Collapsed; } void UpdateCurrentPage() @@ -284,10 +330,7 @@ namespace Xamarin.Forms.Platform.UWP Page page = Element.CurrentPage; var nav = page as NavigationPage; - ((ITitleProvider)this).ShowTitle = nav != null; - - UpdateBarTextColor(); - UpdateBarBackgroundColor(); + TitleProvider.ShowTitle = nav != null; if (page == null) return; -- cgit v1.2.3