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 14, 2024
1 parent abec5a8 commit 74d632f
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ void HandlePropertyChanged(object sender, PropertyChangedEventArgs e)
e.PropertyName == NavigationPage.BarBackgroundProperty.PropertyName)
{
UpdateBarBackground();
UpdateToolbarItems();
}
else if (e.PropertyName == NavigationPage.BarTextColorProperty.PropertyName
|| e.PropertyName == StatusBarTextColorModeProperty.PropertyName)
Expand All @@ -484,6 +485,7 @@ void HandlePropertyChanged(object sender, PropertyChangedEventArgs e)
else if (e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName)
{
UpdateBackgroundColor();
UpdateToolbarItems();
}
else if (e.PropertyName == NavigationPage.CurrentPageProperty.PropertyName)
{
Expand Down Expand Up @@ -710,6 +712,12 @@ void UpdateBackgroundColor()
View.BackgroundColor = color;
}

void UpdateToolbarItems()
{
var pack = (ParentingViewController)TopViewController;
pack?.UpdateToolbarItems();
}

void UpdateBarBackground()
{
var barBackgroundColor = NavPage.BarBackgroundColor;
Expand Down Expand Up @@ -1620,7 +1628,7 @@ void UpdateNavigationBarVisibility(bool animated)
}
}

void UpdateToolbarItems()
internal void UpdateToolbarItems()
{
if (NavigationItem.RightBarButtonItems != null)
{
Expand Down
2 changes: 2 additions & 0 deletions src/Controls/src/Core/Element/Element.cs
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,8 @@ protected virtual void OnChildAdded(Element child)
{
child.SetParent(this);

child.ApplyBindings();

ChildAdded?.Invoke(this, new ElementEventArgs(child));

VisualDiagnostics.OnChildAdded(this, child);
Expand Down
31 changes: 31 additions & 0 deletions src/Controls/tests/TestCases.HostApp/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.HostApp/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;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#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]
[Category(UITestCategories.Page)]
public void Issue23195Test()
{
App.WaitForElement("button");
App.Click("button");
App.WaitForElement("label");
VerifyScreenshot();
}
}
}
#endif
16 changes: 16 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui23195.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Name="contentPage"
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.Maui23195">
<ContentPage.ToolbarItems>
<ToolbarItem>
<ToolbarItem.IconImageSource>
<FontImageSource Glyph="&#xf133;"
x:Name="fontImage"
Color="{AppThemeBinding Light=Black, Dark=White}"/>
</ToolbarItem.IconImageSource>
</ToolbarItem>
</ContentPage.ToolbarItems>
</ContentPage>
50 changes: 50 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui23195.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Controls.Core.UnitTests;
using Microsoft.Maui.Dispatching;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.UnitTests;
using NUnit.Framework;

namespace Microsoft.Maui.Controls.Xaml.UnitTests
{
public partial class Maui23195 : ContentPage
{
public Maui23195()
{
InitializeComponent();
}

public Maui23195(bool useCompiledXaml)
{
//this stub will be replaced at compile time
}

[TestFixture]
class Test
{
[SetUp]
public void Setup()
{
Application.SetCurrentApplication(new MockApplication());
DispatcherProvider.SetCurrent(new DispatcherProviderStub());
}


[TearDown] public void TearDown() => AppInfo.SetCurrent(null);

[Test]
public void FontImageColorShouldChangeOnAppThemeChange([Values(false, true)] bool useCompiledXaml)
{
var contentPage = new Maui23195(useCompiledXaml);
Application.Current.MainPage = contentPage;
Assert.True(contentPage.fontImage.Parent is not null);

Application.Current.UserAppTheme = AppTheme.Light;
Assert.AreEqual(contentPage.fontImage.Color, Colors.Black);

Application.Current.UserAppTheme = AppTheme.Dark;
Assert.AreEqual(contentPage.fontImage.Color, Colors.White);
}
}
}
}

0 comments on commit 74d632f

Please sign in to comment.