From 79535959568f57fa3fbeae7c05bf0415637d06da Mon Sep 17 00:00:00 2001 From: Emmanuel Hansen Date: Fri, 10 Apr 2026 13:38:17 +0000 Subject: [PATCH] Fix System.InvalidCastException in PageNavigationHost (#21144) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix invalid cast exception in PageNavigationHost * Added tests --------- Co-authored-by: Javier Suárez Ruiz --- .../Page/PageNavigationHost.cs | 8 ++--- .../PageNavigationHostTests.cs | 33 +++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/Avalonia.Controls/Page/PageNavigationHost.cs b/src/Avalonia.Controls/Page/PageNavigationHost.cs index 025a8166ab..b02334d59e 100644 --- a/src/Avalonia.Controls/Page/PageNavigationHost.cs +++ b/src/Avalonia.Controls/Page/PageNavigationHost.cs @@ -114,8 +114,8 @@ namespace Avalonia.Controls if (change.Property == PageProperty) { - var oldPage = change.GetOldValue(); - var newPage = change.GetNewValue(); + var oldPage = change.OldValue as Page; + var newPage = change.NewValue as Page; SetCurrentValue(ContentProperty, newPage); @@ -132,10 +132,10 @@ namespace Avalonia.Controls if (e.Property != ContentPresenter.ChildProperty) return; - if (e.GetOldValue() is Page oldPage) + if (e.OldValue is Page oldPage) oldPage.SafeAreaPadding = default; - if (e.GetNewValue() is Page newPage && _insetManager != null) + if (e.NewValue is Page newPage && _insetManager != null) newPage.SafeAreaPadding = _insetManager.SafeAreaPadding; } diff --git a/tests/Avalonia.Controls.UnitTests/PageNavigationHostTests.cs b/tests/Avalonia.Controls.UnitTests/PageNavigationHostTests.cs index 862a407d3d..88b8741a03 100644 --- a/tests/Avalonia.Controls.UnitTests/PageNavigationHostTests.cs +++ b/tests/Avalonia.Controls.UnitTests/PageNavigationHostTests.cs @@ -129,5 +129,38 @@ public class PageNavigationHostTests Assert.Equal("NavigatedFrom", order[0]); Assert.Equal("NavigatedTo", order[1]); } + + [Fact] + public void InitialLayout_WithExistingPage_DoesNotThrow_WhenContentPresenterChildIsAssigned() + { + var page = new ContentPage { Header = "Home" }; + var host = new PageNavigationHost { Page = page }; + var root = new TestRoot { Child = host }; + + var exception = Record.Exception(() => root.LayoutManager.ExecuteInitialLayoutPass()); + + Assert.Null(exception); + Assert.NotNull(host.Presenter); + Assert.Same(page, host.Presenter!.Child); + } + + [Fact] + public void ReplacingPage_ResetsOldPresenterChildSafeAreaPadding() + { + var first = new ContentPage { Header = "First" }; + var second = new ContentPage { Header = "Second" }; + var host = new PageNavigationHost { Page = first }; + var root = new TestRoot { Child = host }; + + root.LayoutManager.ExecuteInitialLayoutPass(); + first.SafeAreaPadding = new Thickness(1, 2, 3, 4); + + var exception = Record.Exception(() => host.Page = second); + + Assert.Null(exception); + Assert.Equal(default, first.SafeAreaPadding); + Assert.NotNull(host.Presenter); + Assert.Same(second, host.Presenter!.Child); + } } }