diff --git a/src/Controls/src/Core/NavigationPage/NavigationPageToolbar.cs b/src/Controls/src/Core/NavigationPage/NavigationPageToolbar.cs index f51ff1e353..f2dd44012c 100644 --- a/src/Controls/src/Core/NavigationPage/NavigationPageToolbar.cs +++ b/src/Controls/src/Core/NavigationPage/NavigationPageToolbar.cs @@ -33,7 +33,6 @@ namespace Microsoft.Maui.Controls _toolbarTracker.CollectionChanged += OnToolbarItemsChanged; RootPage = rootPage; _toolbarTracker.PageAppearing += OnPageAppearing; - _toolbarTracker.PagePropertyChanged += OnPagePropertyChanged; _toolbarTracker.Target = RootPage; } @@ -71,6 +70,7 @@ namespace Microsoft.Maui.Controls if (sender is not ContentPage cp) return; + _toolbarTracker.PagePropertyChanged -= OnPagePropertyChanged; _currentPage = cp; _currentNavigationPage = _currentPage.FindParentOfType(); @@ -112,6 +112,7 @@ namespace Microsoft.Maui.Controls _hasAppeared = true; ApplyChanges(_currentNavigationPage); + _toolbarTracker.PagePropertyChanged += OnPagePropertyChanged; } // This is to catch scenarios where the user diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue25114.cs b/src/Controls/tests/TestCases.HostApp/Issues/Issue25114.cs new file mode 100644 index 0000000000..5a3f379c9d --- /dev/null +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue25114.cs @@ -0,0 +1,40 @@ +namespace Maui.Controls.Sample.Issues; +using Microsoft.Maui.Controls; + +[Issue(IssueTracker.Github, 25114, "NullReferenceException when setting BarBackgroundColor for a NavigationPage", PlatformAffected.All)] +public class Issue25114Flyout : FlyoutPage +{ + public Issue25114Flyout() + { + var navPage = new NavigationPage(new Issue25114()); + navPage.SetDynamicResource(NavigationPage.BarBackgroundColorProperty, "Primary"); + Detail = navPage; + Flyout = new ContentPage + { + Title = "Flyout" + }; + } +} + +public class Issue25114 : ContentPage +{ + public Issue25114() + { + Content = new Button() + { + AutomationId = "button", + HeightRequest = 100, + Text = "Change the Navigation Bar's Color", + Command = new Command(() => + { + Application.Current.Resources["Primary"] = "#0000FF"; + }) + }; + } + + protected override void OnAppearing() + { + base.OnAppearing(); + Application.Current.Resources["Primary"] = "#00FF00"; + } +} \ No newline at end of file diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue25114.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue25114.cs new file mode 100644 index 0000000000..eff37c11e0 --- /dev/null +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue25114.cs @@ -0,0 +1,23 @@ +using NUnit.Framework; +using UITest.Appium; +using UITest.Core; + +namespace Microsoft.Maui.TestCases.Tests.Issues; + +public class Issue25114 : _IssuesUITest +{ + public Issue25114(TestDevice testDevice) : base(testDevice) + { + } + + public override string Issue => "NullReferenceException when setting BarBackgroundColor for a NavigationPage"; + + [Test] + [Category(UITestCategories.FlyoutPage)] + public void NoExceptionShouldBeThrownWhenChangingNavigationBarColor() + { + App.WaitForElement("button"); + App.Tap("button"); + } + +} \ No newline at end of file