Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/Sentry.Maui/Internal/PageNavigationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace Sentry.Maui.Internal;

internal static class PageNavigationExtensions
{
#if !NET10_0_OR_GREATER
private static readonly PropertyInfo? DestinationPageProperty;
private static readonly PropertyInfo? PreviousPageProperty;

Expand All @@ -15,22 +16,29 @@ static PageNavigationExtensions()
return;
}
DestinationPageProperty = typeof(NavigatedFromEventArgs)
.GetProperty("DestinationPage", BindingFlags.Instance | BindingFlags.NonPublic);
.GetProperty("DestinationPage", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the #if !NET10_0_OR_GREATER above, do we need to include BindingFlags.Public | here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, yes I just checked again and DestinationPage and PreviousPage are internal a per:
https://github.com/dotnet/maui/pull/28384/changes#diff-d319c21ee4f050d6e54857fe31878d25c8bcbbd8d24479e1d63f4edf68d5341e

so BindingFlags.Instance | BindingFlags.NonPublic is fine

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved by 38292b8

PreviousPageProperty = typeof(NavigatedToEventArgs)
.GetProperty("PreviousPage", BindingFlags.Instance | BindingFlags.NonPublic);
.GetProperty("PreviousPage", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
}
#endif

/// <summary>
/// Reads the (internal) NavigatedFromEventArgs.DestinationPage property via reflection.
/// Note that this will return null if trimming is enabled.
/// Gets the destination page from navigation arguments.
/// </summary>
public static Page? GetDestinationPage(this NavigatedFromEventArgs eventArgs) =>
#if NET10_0_OR_GREATER
eventArgs.DestinationPage;
#else
DestinationPageProperty?.GetValue(eventArgs) as Page;
#endif

/// <summary>
/// Reads the (internal) NavigatedFromEventArgs.PreviousPage property via reflection.
/// Note that this will return null if trimming is enabled.
/// Gets the previous page from navigation arguments.
/// </summary>
public static Page? GetPreviousPage(this NavigatedToEventArgs eventArgs) =>
#if NET10_0_OR_GREATER
eventArgs.PreviousPage;
#else
PreviousPageProperty?.GetValue(eventArgs) as Page;
#endif
}
3 changes: 0 additions & 3 deletions test/Sentry.Maui.Tests/MauiEventsBinderTests.Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,8 @@ public void Page_NavigatedTo_AddsBreadcrumb()
Assert.Equal(MauiEventsBinder.NavigationType, crumb.Type);
Assert.Equal(MauiEventsBinder.NavigationCategory, crumb.Category);
crumb.Data.Should().Contain($"{nameof(Page)}.Name", "page");
#if !NET10_0
// TODO: Work out why these are missing in .NET 10
crumb.Data.Should().Contain("PreviousPage", nameof(Page));
crumb.Data.Should().Contain("PreviousPage.Name", "otherPage");
#endif
}

[Fact]
Expand Down
Loading