Skip to content

Commit

Permalink
Navigationbar BarBackgroundColor is not updating (dotnet#23195)
Browse files Browse the repository at this point in the history
  • Loading branch information
kubaflo committed Sep 4, 2024
1 parent c156658 commit 897b2fe
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,8 @@ void HandlePropertyChanged(object sender, PropertyChangedEventArgs e)
e.PropertyName == NavigationPage.BarBackgroundProperty.PropertyName)
{
UpdateBarBackground();
var pack = (ParentingViewController)TopViewController;
pack?.UpdateToolbarItems();
}
else if (e.PropertyName == NavigationPage.BarTextColorProperty.PropertyName
|| e.PropertyName == StatusBarTextColorModeProperty.PropertyName)
Expand All @@ -481,6 +483,8 @@ void HandlePropertyChanged(object sender, PropertyChangedEventArgs e)
else if (e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName)
{
UpdateBackgroundColor();
var pack = (ParentingViewController)TopViewController;
pack?.UpdateToolbarItems();
}
else if (e.PropertyName == NavigationPage.CurrentPageProperty.PropertyName)
{
Expand Down Expand Up @@ -1605,7 +1609,7 @@ void UpdateNavigationBarVisibility(bool animated)
}
}

void UpdateToolbarItems()
internal void UpdateToolbarItems()
{
if (NavigationItem.RightBarButtonItems != null)
{
Expand Down
7 changes: 6 additions & 1 deletion src/Controls/src/Core/Menu/MenuItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ public partial class MenuItem : BaseMenuItem, IMenuItemController, IStyleSelecta
public static readonly BindableProperty IsDestructiveProperty = BindableProperty.Create(nameof(IsDestructive), typeof(bool), typeof(MenuItem), false);

/// <summary>Bindable property for <see cref="IconImageSource"/>.</summary>
public static readonly BindableProperty IconImageSourceProperty = BindableProperty.Create(nameof(IconImageSource), typeof(ImageSource), typeof(MenuItem), default(ImageSource));
public static readonly BindableProperty IconImageSourceProperty = BindableProperty.Create(nameof(IconImageSource), typeof(ImageSource), typeof(MenuItem), default(ImageSource),
propertyChanged: (bindable, oldValue, newValue) => {
var menuItem = (MenuItem)bindable;
var icon = (ImageSource)newValue;
icon.Parent = menuItem;
} );

/// <summary>Bindable property for <see cref="IsEnabled"/>.</summary>
public static readonly BindableProperty IsEnabledProperty = BindableProperty.Create(
Expand Down
6 changes: 6 additions & 0 deletions src/Controls/src/Core/NavigationPage/NavigationPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ public NavigationPage(Page root) : this(UseMauiHandler, root)
{
}

internal override void OnParentResourcesChanged(object sender, ResourcesChangedEventArgs e)
{
base.OnParentResourcesChanged(sender, e);
_toolbar.OnParentResourcesChanged();
}

internal NavigationPage(bool setforMaui, Page root = null)
{
_setForMaui = setforMaui;
Expand Down
5 changes: 5 additions & 0 deletions src/Controls/src/Core/NavigationPage/NavigationPageToolbar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ void OnPagePropertyChanged(object sender, System.ComponentModel.PropertyChangedE
}
}

internal void OnParentResourcesChanged()
{
ApplyChanges(_currentNavigationPage);
}

void OnPageAppearing(object sender, EventArgs e)
{
if (sender is not ContentPage cp)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#if IOS || ANDROID
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues
{
public class Issue23195 : _IssuesUITest
{
public override string Issue => "NavigationBarColors from NavigationPage not changing on AppTheme changing";

public Issue23195(TestDevice device) : base(device)
{
}

[Test]
public void Issue23195Test()
{
App.WaitForElement("button");
App.Click("button");
App.WaitForElement("label");
VerifyScreenshot();
}
}
}
#endif
31 changes: 31 additions & 0 deletions src/Controls/tests/TestCases/Issues/Issues23195.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8" ?>
<NavigationPage
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Name="Self"
BarBackgroundColor="{AppThemeBinding Light=White,Dark=Black}"
BarTextColor="{AppThemeBinding Light=Black,Dark=White}"
x:Class="Maui.Controls.Sample.Issues.Issue23195">
<x:Arguments>
<ContentPage x:Name="contentPage" Title="Content page">
<ContentPage.ToolbarItems>
<ToolbarItem>
<ToolbarItem.IconImageSource>
<FontImageSource Glyph="&#xf133;"
Color="{AppThemeBinding Light=Black, Dark=White}"
FontFamily="FA"/>
</ToolbarItem.IconImageSource>
</ToolbarItem>
</ContentPage.ToolbarItems>
<VerticalStackLayout>
<Button Text="Chnage theme"
AutomationId="button"
HeightRequest="100"
Clicked="Button_Clicked"/>
<Label Text="Dark theme applied"
IsVisible="{AppThemeBinding Default=False,Dark=True}"
AutomationId="label"/>
</VerticalStackLayout>
</ContentPage>
</x:Arguments>
</NavigationPage>
18 changes: 18 additions & 0 deletions src/Controls/tests/TestCases/Issues/Issues23195.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Maui.Controls.Sample.Issues
{
[XamlCompilation(XamlCompilationOptions.Compile)]
[Issue(IssueTracker.Github, 23195, "NavigationBarColors from NavigationPage not changing on AppTheme changing", PlatformAffected.Android | PlatformAffected.iOS)]
public partial class Issue23195 : NavigationPage
{
public Issue23195()
{
InitializeComponent();
PushAsync(contentPage);
}

public void Button_Clicked(object sender, EventArgs eventArgs)
{
Application.Current!.UserAppTheme= AppTheme.Dark;
}
}
}

0 comments on commit 897b2fe

Please sign in to comment.