Skip to content

Commit 7c12652

Browse files
committed
Fix missing iOS navigation events and removes duplicate page (dis)appearing events
1 parent e25f0f7 commit 7c12652

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/Sentry.Maui/Internal/MauiEventsBinder.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,16 @@ public void HandleApplicationEvents(Application application, bool bind = true)
3434
{
3535
if (bind)
3636
{
37-
// Attach element events to all descendents as they are added to the application.
37+
// Attach element events to all existing descendants (skip the application itself)
38+
foreach (var descendant in application.GetVisualTreeDescendants().Skip(1))
39+
{
40+
if (descendant is VisualElement element)
41+
{
42+
OnApplicationOnDescendantAdded(application, new ElementEventArgs(element));
43+
}
44+
}
45+
46+
// Attach element events to all descendants as they are added to the application.
3847
application.DescendantAdded += OnApplicationOnDescendantAdded;
3948
application.DescendantRemoved += OnApplicationOnDescendantRemoved;
4049

test/Sentry.Maui.Tests/MauiEventsBinderTests.Application.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,34 @@ public void Application_UnbindModalEvents_DoesNotAddBreadcrumb(string eventName,
145145
Assert.Single(_fixture.Scope.Breadcrumbs);
146146
}
147147

148+
[Fact]
149+
public void Application_HandleApplicationEvents_TracksExistingDescendants()
150+
{
151+
// Arrange
152+
var application = MockApplication.Create();
153+
var element = new MockVisualElement("element");
154+
application.MainPage = new ContentPage
155+
{
156+
Content = new VerticalStackLayout
157+
{
158+
element
159+
}
160+
};
161+
162+
_fixture.Binder.HandleApplicationEvents(application);
163+
164+
// Act
165+
element.RaiseEvent(nameof(VisualElement.Focused), new FocusEventArgs(element, true));
166+
167+
// Assert
168+
var crumb = Assert.Single(_fixture.Scope.Breadcrumbs);
169+
Assert.Equal($"{nameof(MockVisualElement)}.{nameof(VisualElement.Focused)}", crumb.Message);
170+
Assert.Equal(BreadcrumbLevel.Info, crumb.Level);
171+
Assert.Equal(MauiEventsBinder.SystemType, crumb.Type);
172+
Assert.Equal(MauiEventsBinder.RenderingCategory, crumb.Category);
173+
crumb.Data.Should().Contain($"{nameof(MockVisualElement)}.Name", "element");
174+
}
175+
148176
public static IEnumerable<object[]> ApplicationModalEventsData
149177
{
150178
get

0 commit comments

Comments
 (0)