Disconnect property changes from all pages when TabbedPage set to null (#18458)

* - add test

* - add back tear down

* - add test to check property changes

---------

Co-authored-by: Shane Neuville <shneuvil@microsoft.com>
This commit is contained in:
maonaoda 2023-11-30 23:35:48 +09:00 коммит произвёл GitHub
Родитель b1cee478aa
Коммит f267a5c585
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 47 добавлений и 2 удалений

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

@ -0,0 +1,44 @@
using System;
using System.Threading.Tasks;
using Microsoft.Maui;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Graphics;
namespace Maui.Controls.Sample.Issues;
[Issue(IssueTracker.Github, 18457, "Adding/Removing Pages From Removed TabbedPage Causes Crash")]
public class Issue18457 : TestContentPage
{
bool pagePushed = false;
TabbedPage _tabbedPage = new TabbedPage();
protected override void Init()
{
_tabbedPage.Children.Add(new ContentPage() { Title = "tab 1"});
_tabbedPage.Children.Add(new ContentPage() { Title = "tab 2"});
_tabbedPage.Children.Add(new ContentPage() { Title = "tab 3"});
}
protected override async void OnNavigatedTo(NavigatedToEventArgs args)
{
base.OnNavigatedTo(args);
if (!pagePushed)
{
pagePushed = true;
await Navigation.PushAsync(_tabbedPage);
await Navigation.PopAsync();
_tabbedPage.Children.Add(new ContentPage());
_tabbedPage.Children[0].Background = SolidColorBrush.Purple;
_tabbedPage.Children[0].Title = "update title";
_tabbedPage.Children[0].IconImageSource = "dotnet_bot.png";
await Task.Yield();
Content = new VerticalStackLayout(){
Children =
{
new Label() { Text = "This test pushes and pops a TabbedPage, and then modifies the Children on the popped page."},
new Label() { Text = "If the app doesn't crash, this test has passed.", AutomationId = "Success" }
}
};
}
}
}

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

@ -114,8 +114,9 @@ namespace Microsoft.Maui.Controls.Handlers
var activity = _context.GetActivity();
var themeContext = activity;
if (Element != null)
if (Element is not null)
{
Element.InternalChildren.ForEach(page => TeardownPage(page as Page));
((IPageController)Element).InternalChildren.CollectionChanged -= OnChildrenCollectionChanged;
Element.Appearing -= OnTabbedPageAppearing;
Element.Disappearing -= OnTabbedPageDisappearing;
@ -124,7 +125,7 @@ namespace Microsoft.Maui.Controls.Handlers
}
Element = tabbedPage;
if (Element != null)
if (Element is not null)
{
_viewPager.LayoutChange += OnLayoutChanged;
Element.Appearing += OnTabbedPageAppearing;