[BUG] StatusBarColor with AppThemeBinding doesn't reflect changes of light/dark mode on the fly #2392
Replies: 4 comments
-
I created another repo based on @jfversluis MauiToolkitStatusBarBehaviorSample: https://github.com/jakob-bmd/MauiToolkitStatusBarBehaviorAppThemeBindingBug/tree/main Steps To Reproduce Expected Behavior Additional Info Environment
|
Beta Was this translation helpful? Give feedback.
-
My current (crude) workaround is subscribing to the RequestedThemeChanged event and manually setting the colors and styles but I feel like this functionality should be in the Behavior itself.
|
Beta Was this translation helpful? Give feedback.
-
Hi @vniehues, thanks so much for reporting this issue! It seems that this isn't a bug, but rather a topic for discussion around integrating a feature to detect when a requested theme has changed. Because of this, I’m moving this to a discussion. If it gains enough interest and votes, it can be considered for future implementation. |
Beta Was this translation helpful? Give feedback.
-
This is another workaround based on being able to call the C# methods directly: CommunityToolkit.Maui.Core.Platform.StatusBar.SetColor(statusBarColor);
CommunityToolkit.Maui.Core.Platform.StatusBar.SetStyle(StatusBarStyle.LightContent); With this, we can introduce StatusBarColor and StatusBarStyle as BindableProperties on your ContentPage, e.g. // MainPage.xaml
public partial class MainPage : ContentPage
{
public static readonly BindableProperty StatusBarColorProperty = BindableProperty.Create(
nameof(StatusBarColor),
typeof(Color),
typeof(MainPage),
Colors.Black,
propertyChanged: (b, o, n) => ((MainPage)b).OnSetStatusBarColor());
public Color StatusBarColor
{
get => (Color)GetValue(StatusBarColorProperty);
set => SetValue(StatusBarColorProperty, value);
}
private void OnSetStatusBarColor()
{
#if ANDROID || IOS
if (StatusBarColor is Color color)
{
CommunityToolkit.Maui.Core.Platform.StatusBar.SetColor(color);
}
#endif
}
public static readonly BindableProperty StatusBarStyleProperty = BindableProperty.Create(
nameof(StatusBarStyle),
typeof(StatusBarStyle),
typeof(MainPage),
StatusBarStyle.DarkContent,
propertyChanged: (b, o, n) => ((MainPage)b).OnSetStatusBarStyle());
public StatusBarStyle StatusBarStyle
{
get => (StatusBarStyle)GetValue(StatusBarStyleProperty);
set => SetValue(StatusBarStyleProperty, value);
}
private void OnSetStatusBarStyle()
{
#if ANDROID || IOS
if (StatusBarStyle is StatusBarStyle style)
{
CommunityToolkit.Maui.Core.Platform.StatusBar.SetStyle(style);
}
#endif
}
public MainPage()
{
OnStatusBarColor();
OnStatusBarStyle();
InitializeComponent();
}
} Then, you can happily use AppThemeBinding in XAML: <ContentPage
x:Class="Maui.StatusBarDemo.MainPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
StatusBarColor="{AppThemeBinding Light=Black, Dark=White}"
StatusBarStyle="{AppThemeBinding Light=LightContent, Dark=DarkContent}">
<!-- ... -->
</ContentPage> The next logical step is to refactor this into a reusable BaseContentPage. |
Beta Was this translation helpful? Give feedback.
-
Is there an existing issue for this?
Did you read the "Reporting a bug" section on Contributing file?
Current Behavior
When setting the StatusBarColor with AppThemeBinding, changes of light/dark mode doesn't get reflected on the fly!
The page needs to be navigated to once more for the changes to take effect!
Screen_Recording_20241028_1000142.mp4
Expected Behavior
Changes should take effect on the fly!
Steps To Reproduce
Link to public reproduction project repository
https://github.com/RsZoli/GitHubRepros
Environment
android 34.0.138/8.0.100 SDK 8.0.400, VS 17.11.35327.3
ios 18.0.8303/8.0.100 SDK 8.0.400, VS 17.11.35327.3
maui-windows 8.0.82/8.0.100 SDK 8.0.400, VS 17.11.35327.3
Anything else?
No response
Beta Was this translation helpful? Give feedback.
All reactions