My UWP Application Title Bar isn't changing its foreground color in Xamarin.Forms

邮差的信 提交于 2021-02-10 17:33:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!