Skip to content

Commit

Permalink
[release/8.0.1xx-sr9] NullReferenceException when setting BarBackgrou…
Browse files Browse the repository at this point in the history
…ndColor 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>
  • Loading branch information
PureWeen and kubaflo authored Oct 14, 2024
1 parent 0294301 commit 006fefb
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public NavigationPageToolbar(Maui.IElement parent, Page rootPage) : base(parent)
_toolbarTracker.CollectionChanged += OnToolbarItemsChanged;
RootPage = rootPage;
_toolbarTracker.PageAppearing += OnPageAppearing;
_toolbarTracker.PagePropertyChanged += OnPagePropertyChanged;
_toolbarTracker.Target = RootPage;
}

Expand Down Expand Up @@ -71,6 +70,7 @@ void OnPageAppearing(object sender, EventArgs e)
if (sender is not ContentPage cp)
return;

_toolbarTracker.PagePropertyChanged -= OnPagePropertyChanged;
_currentPage = cp;
_currentNavigationPage = _currentPage.FindParentOfType<NavigationPage>();

Expand Down Expand Up @@ -112,6 +112,7 @@ void OnPageAppearing(object sender, EventArgs e)
_hasAppeared = true;

ApplyChanges(_currentNavigationPage);
_toolbarTracker.PagePropertyChanged += OnPagePropertyChanged;
}

// This is to catch scenarios where the user
Expand Down
40 changes: 40 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue25114.cs
Original file line number Diff line number Diff line change
@@ -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";
}
}
Original file line number Diff line number Diff line change
@@ -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");
}

}

0 comments on commit 006fefb

Please sign in to comment.