NullReferenceException when setting BarBackgroundColor for a NavigationPage - fix (#25197)

* Added a null check

* Added a UI test

* Removed an xaml file for ui test

* Update NavigationPageToolbar.cs

* Improvements

* Update NavigationPageToolbar.cs
This commit is contained in:
Jakub Florkowski 2024-10-15 16:52:46 +02:00 коммит произвёл GitHub
Родитель dbeb2dda0c
Коммит 7f292e66c8
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
3 изменённых файлов: 65 добавлений и 1 удалений

Просмотреть файл

@ -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<NavigationPage>();
@ -112,6 +112,7 @@ namespace Microsoft.Maui.Controls
_hasAppeared = true;
ApplyChanges(_currentNavigationPage);
_toolbarTracker.PagePropertyChanged += OnPagePropertyChanged;
}
// This is to catch scenarios where the user

Просмотреть файл

@ -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";
}
}

Просмотреть файл

@ -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");
}
}