From 0cc2fd2b6742a29fedc03de942801cc14ff6b499 Mon Sep 17 00:00:00 2001 From: Samantha Houts Date: Tue, 15 Nov 2016 11:43:34 -0800 Subject: [PATCH] [UWP] Explicitly set mobile StatusBar colors to white Background/black Foreground on Light theme (#491) * [UWP] Encapsulate MobileStatusBar * [UWP] Set StatusBar colors on Light theme * Add test code to override status bar color --- .../App.xaml | 3 ++- .../App.xaml.cs | 20 ++++++++++++++-- ...in.Forms.ControlGallery.WindowsUniversal.csproj | 5 ++++ Xamarin.Forms.Platform.WinRT/Platform.cs | 28 +++++++++++++++++----- 4 files changed, 47 insertions(+), 9 deletions(-) diff --git a/Xamarin.Forms.ControlGallery.WindowsUniversal/App.xaml b/Xamarin.Forms.ControlGallery.WindowsUniversal/App.xaml index 89b2c56..d114d1a 100644 --- a/Xamarin.Forms.ControlGallery.WindowsUniversal/App.xaml +++ b/Xamarin.Forms.ControlGallery.WindowsUniversal/App.xaml @@ -2,7 +2,8 @@ x:Class="Xamarin.Forms.ControlGallery.WindowsUniversal.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" - xmlns:local="using:Xamarin.Forms.ControlGallery.WindowsUniversal"> + xmlns:local="using:Xamarin.Forms.ControlGallery.WindowsUniversal" + RequestedTheme="Light"> diff --git a/Xamarin.Forms.ControlGallery.WindowsUniversal/App.xaml.cs b/Xamarin.Forms.ControlGallery.WindowsUniversal/App.xaml.cs index 6253644..e53b666 100644 --- a/Xamarin.Forms.ControlGallery.WindowsUniversal/App.xaml.cs +++ b/Xamarin.Forms.ControlGallery.WindowsUniversal/App.xaml.cs @@ -7,6 +7,9 @@ using Windows.ApplicationModel; using Windows.ApplicationModel.Activation; using Windows.Foundation; using Windows.Foundation.Collections; +using Windows.Foundation.Metadata; +using Windows.UI; +using Windows.UI.ViewManagement; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; @@ -76,8 +79,21 @@ namespace Xamarin.Forms.ControlGallery.WindowsUniversal // parameter rootFrame.Navigate(typeof(MainPage), e.Arguments); } - // Ensure the current window is active - Window.Current.Activate(); + + //// Uncomment to test overriding the status bar color + //if (ApiInformation.IsTypePresent("Windows.UI.ViewManagement.StatusBar")) + //{ + // var statusBar = StatusBar.GetForCurrentView(); + // if (statusBar != null) + // { + // statusBar.BackgroundOpacity = 1; + // statusBar.BackgroundColor = Colors.Black; + // statusBar.ForegroundColor = Colors.White; + // } + //} + + // Ensure the current window is active + Window.Current.Activate(); } /// diff --git a/Xamarin.Forms.ControlGallery.WindowsUniversal/Xamarin.Forms.ControlGallery.WindowsUniversal.csproj b/Xamarin.Forms.ControlGallery.WindowsUniversal/Xamarin.Forms.ControlGallery.WindowsUniversal.csproj index 217fa0a..7f0162f 100644 --- a/Xamarin.Forms.ControlGallery.WindowsUniversal/Xamarin.Forms.ControlGallery.WindowsUniversal.csproj +++ b/Xamarin.Forms.ControlGallery.WindowsUniversal/Xamarin.Forms.ControlGallery.WindowsUniversal.csproj @@ -193,6 +193,11 @@ Designer + + + Windows Mobile Extensions for the UWP + + 14.0 diff --git a/Xamarin.Forms.Platform.WinRT/Platform.cs b/Xamarin.Forms.Platform.WinRT/Platform.cs index e5a989c..d6a68fe 100644 --- a/Xamarin.Forms.Platform.WinRT/Platform.cs +++ b/Xamarin.Forms.Platform.WinRT/Platform.cs @@ -32,6 +32,10 @@ namespace Xamarin.Forms.Platform.WinRT { internal static readonly BindableProperty RendererProperty = BindableProperty.CreateAttached("Renderer", typeof(IVisualElementRenderer), typeof(Platform), default(IVisualElementRenderer)); +#if WINDOWS_UWP + internal static StatusBar MobileStatusBar => ApiInformation.IsTypePresent("Windows.UI.ViewManagement.StatusBar") ? StatusBar.GetForCurrentView() : null; +#endif + public static IVisualElementRenderer GetRenderer(VisualElement element) { return (IVisualElementRenderer)element.GetValue(RendererProperty); @@ -79,13 +83,26 @@ namespace Xamarin.Forms.Platform.WinRT UpdateBounds(); - #if WINDOWS_UWP - if (ApiInformation.IsTypePresent("Windows.UI.ViewManagement.StatusBar")) + StatusBar statusBar = MobileStatusBar; + if (statusBar != null) { - StatusBar statusBar = StatusBar.GetForCurrentView(); statusBar.Showing += (sender, args) => UpdateBounds(); statusBar.Hiding += (sender, args) => UpdateBounds(); + + // UWP 14393 Bug: If RequestedTheme is Light (which it is by default), then the + // status bar uses White Foreground with White Background. + // UWP 10586 Bug: If RequestedTheme is Light (which it is by default), then the + // status bar uses Black Foreground with Black Background. + // Since the Light theme should have a Black on White status bar, we will set it explicitly. + // This can be overriden by setting the status bar colors in App.xaml.cs OnLaunched. + + if (statusBar.BackgroundColor == null && statusBar.ForegroundColor == null && Windows.UI.Xaml.Application.Current.RequestedTheme == ApplicationTheme.Light) + { + statusBar.BackgroundColor = Colors.White; + statusBar.ForegroundColor = Colors.Black; + statusBar.BackgroundOpacity = 1; + } } #endif } @@ -420,10 +437,9 @@ namespace Xamarin.Forms.Platform.WinRT { _bounds = new Rectangle(0, 0, _page.ActualWidth, _page.ActualHeight); #if WINDOWS_UWP - if (ApiInformation.IsTypePresent("Windows.UI.ViewManagement.StatusBar")) + StatusBar statusBar = MobileStatusBar; + if (statusBar != null) { - StatusBar statusBar = StatusBar.GetForCurrentView(); - bool landscape = Device.Info.CurrentOrientation.IsLandscape(); bool titleBar = CoreApplication.GetCurrentView().TitleBar.IsVisible; double offset = landscape ? statusBar.OccludedRect.Width : statusBar.OccludedRect.Height; -- 2.7.4