问题
I'm working on a cross platform app with Xamarin.Forms and on the UWP side, I added this code in MainPage.xaml.cs to change the app title bar to match the navigation bar I'm using:
var titleBar = ApplicationView.GetForCurrentView().TitleBar;
titleBar.BackgroundColor = Colors.LightSlateGray;
titleBar.ForegroundColor = Colors.White;
titleBar.InactiveBackgroundColor = Colors.LightSlateGray;
titleBar.InactiveForegroundColor = Colors.White;
titleBar.ButtonBackgroundColor = Colors.LightSlateGray;
titleBar.ButtonForegroundColor = Colors.White;
titleBar.ButtonInactiveBackgroundColor = Colors.LightSlateGray;
titleBar.ButtonInactiveForegroundColor = Colors.White;
However, the foreground color is black when the app is active:
When the app is inactive, it actually looks correct:
I've actually noticed that the text shows up as white for a fraction of a second before switching to black. Should I be running this code later, or somewhere else? I've tried before and after LoadApplication. Am I missing something, or is there a better way to handle this in Xamarin?
回答1:
So it seems there may be a bug with setting the foreground color (when active) to exactly white. Perhaps something Xamarin is doing cancels it out. I found that picking a color that is close to white works just fine, such as this:
var titleBar = ApplicationView.GetForCurrentView().TitleBar;
titleBar.BackgroundColor = Colors.LightSlateGray;
titleBar.ForegroundColor = Colors.Snow;
titleBar.InactiveBackgroundColor = Colors.LightSlateGray;
titleBar.InactiveForegroundColor = Colors.White;
titleBar.ButtonBackgroundColor = Colors.LightSlateGray;
titleBar.ButtonForegroundColor = Colors.Snow;
titleBar.ButtonInactiveBackgroundColor = Colors.LightSlateGray;
titleBar.ButtonInactiveForegroundColor = Colors.White;
回答2:
Try placing in your UWP app's OnLaunched method (App.xaml.cs), after the call to Window.Activate.
来源:https://stackoverflow.com/questions/57024844/my-uwp-application-title-bar-isnt-changing-its-foreground-color-in-xamarin-form