Skip to content

Commit 006fefb

Browse files
PureWeenkubaflo
andauthored
[release/8.0.1xx-sr9] NullReferenceException when setting BarBackgroundColor for a NavigationPage - fix (#25251)
* Added a null check * Added a UI test * Removed an xaml file for ui test * Update NavigationPageToolbar.cs * Improvements * Update NavigationPageToolbar.cs --------- Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com>
1 parent 0294301 commit 006fefb

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

src/Controls/src/Core/NavigationPage/NavigationPageToolbar.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public NavigationPageToolbar(Maui.IElement parent, Page rootPage) : base(parent)
3333
_toolbarTracker.CollectionChanged += OnToolbarItemsChanged;
3434
RootPage = rootPage;
3535
_toolbarTracker.PageAppearing += OnPageAppearing;
36-
_toolbarTracker.PagePropertyChanged += OnPagePropertyChanged;
3736
_toolbarTracker.Target = RootPage;
3837
}
3938

@@ -71,6 +70,7 @@ void OnPageAppearing(object sender, EventArgs e)
7170
if (sender is not ContentPage cp)
7271
return;
7372

73+
_toolbarTracker.PagePropertyChanged -= OnPagePropertyChanged;
7474
_currentPage = cp;
7575
_currentNavigationPage = _currentPage.FindParentOfType<NavigationPage>();
7676

@@ -112,6 +112,7 @@ void OnPageAppearing(object sender, EventArgs e)
112112
_hasAppeared = true;
113113

114114
ApplyChanges(_currentNavigationPage);
115+
_toolbarTracker.PagePropertyChanged += OnPagePropertyChanged;
115116
}
116117

117118
// This is to catch scenarios where the user
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
namespace Maui.Controls.Sample.Issues;
2+
using Microsoft.Maui.Controls;
3+
4+
[Issue(IssueTracker.Github, 25114, "NullReferenceException when setting BarBackgroundColor for a NavigationPage", PlatformAffected.All)]
5+
public class Issue25114Flyout : FlyoutPage
6+
{
7+
public Issue25114Flyout()
8+
{
9+
var navPage = new NavigationPage(new Issue25114());
10+
navPage.SetDynamicResource(NavigationPage.BarBackgroundColorProperty, "Primary");
11+
Detail = navPage;
12+
Flyout = new ContentPage
13+
{
14+
Title = "Flyout"
15+
};
16+
}
17+
}
18+
19+
public class Issue25114 : ContentPage
20+
{
21+
public Issue25114()
22+
{
23+
Content = new Button()
24+
{
25+
AutomationId = "button",
26+
HeightRequest = 100,
27+
Text = "Change the Navigation Bar's Color",
28+
Command = new Command(() =>
29+
{
30+
Application.Current.Resources["Primary"] = "#0000FF";
31+
})
32+
};
33+
}
34+
35+
protected override void OnAppearing()
36+
{
37+
base.OnAppearing();
38+
Application.Current.Resources["Primary"] = "#00FF00";
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using NUnit.Framework;
2+
using UITest.Appium;
3+
using UITest.Core;
4+
5+
namespace Microsoft.Maui.TestCases.Tests.Issues;
6+
7+
public class Issue25114 : _IssuesUITest
8+
{
9+
public Issue25114(TestDevice testDevice) : base(testDevice)
10+
{
11+
}
12+
13+
public override string Issue => "NullReferenceException when setting BarBackgroundColor for a NavigationPage";
14+
15+
[Test]
16+
[Category(UITestCategories.FlyoutPage)]
17+
public void NoExceptionShouldBeThrownWhenChangingNavigationBarColor()
18+
{
19+
App.WaitForElement("button");
20+
App.Tap("button");
21+
}
22+
23+
}

0 commit comments

Comments
 (0)