@@ -34,9 +34,6 @@ public MauiEventsBinder(IHub hub, IOptions<SentryMauiOptions> options, IEnumerab
3434
3535 public void HandleApplicationEvents ( Application application , bool bind = true )
3636 {
37- // we always unbind first to ensure no previous hooks
38- UnbindApplication ( application ) ;
39-
4037 if ( bind )
4138 {
4239 // Attach element events to all existing descendants (skip the application itself)
@@ -68,23 +65,22 @@ public void HandleApplicationEvents(Application application, bool bind = true)
6865 // https://docs.microsoft.com/dotnet/maui/user-interface/system-theme-changes#react-to-theme-changes
6966 application . RequestedThemeChanged += OnApplicationOnRequestedThemeChanged ;
7067 }
71- }
72-
73- private void UnbindApplication ( Application application )
74- {
75- application . DescendantAdded -= OnApplicationOnDescendantAdded ;
76- application . DescendantRemoved -= OnApplicationOnDescendantRemoved ;
68+ else
69+ {
70+ application . DescendantAdded -= OnApplicationOnDescendantAdded ;
71+ application . DescendantRemoved -= OnApplicationOnDescendantRemoved ;
7772
78- HandleElementEvents ( application , bind : false ) ;
73+ HandleElementEvents ( application , bind : false ) ;
7974
80- // Navigation events
81- application . PageAppearing -= OnApplicationOnPageAppearing ;
82- application . PageDisappearing -= OnApplicationOnPageDisappearing ;
83- application . ModalPushed -= OnApplicationOnModalPushed ;
84- application . ModalPopped -= OnApplicationOnModalPopped ;
75+ // Navigation events
76+ application . PageAppearing -= OnApplicationOnPageAppearing ;
77+ application . PageDisappearing -= OnApplicationOnPageDisappearing ;
78+ application . ModalPushed -= OnApplicationOnModalPushed ;
79+ application . ModalPopped -= OnApplicationOnModalPopped ;
8580
86- // Theme changed event
87- application . RequestedThemeChanged -= OnApplicationOnRequestedThemeChanged ;
81+ // Theme changed event
82+ application . RequestedThemeChanged -= OnApplicationOnRequestedThemeChanged ;
83+ }
8884 }
8985
9086 internal void OnApplicationOnDescendantAdded ( object ? _ , ElementEventArgs e )
@@ -173,7 +169,6 @@ internal void OnApplicationOnDescendantRemoved(object? _, ElementEventArgs e)
173169
174170 internal void HandleWindowEvents ( Window window , bool bind = true )
175171 {
176- UnhookWindow ( window ) ;
177172 if ( bind )
178173 {
179174 // Lifecycle Events
@@ -197,39 +192,29 @@ internal void HandleWindowEvents(Window window, bool bind = true)
197192 window . ModalPopped += OnWindowOnModalPopped ;
198193 window . PopCanceled += OnWindowOnPopCanceled ;
199194 }
200- }
195+ else
196+ {
197+ // Lifecycle events caused by user action
198+ window . Activated -= OnWindowOnActivated ;
199+ window . Deactivated -= OnWindowOnDeactivated ;
200+ window . Stopped -= OnWindowOnStopped ;
201+ window . Resumed -= OnWindowOnResumed ;
201202
202- private void UnhookWindow ( Window window )
203- {
204- // Lifecycle events caused by user action
205- window . Activated -= OnWindowOnActivated ;
206- window . Deactivated -= OnWindowOnDeactivated ;
207- window . Stopped -= OnWindowOnStopped ;
208- window . Resumed -= OnWindowOnResumed ;
209-
210- // System generated lifecycle events
211- window . Created -= OnWindowOnCreated ;
212- window . Destroying -= OnWindowOnDestroying ;
213- window . Backgrounding -= OnWindowOnBackgrounding ;
214- window . DisplayDensityChanged -= OnWindowOnDisplayDensityChanged ;
215-
216- // Navigation events
217- window . ModalPushed -= OnWindowOnModalPushed ;
218- window . ModalPopped -= OnWindowOnModalPopped ;
219- window . PopCanceled -= OnWindowOnPopCanceled ;
203+ // System generated lifecycle events
204+ window . Created -= OnWindowOnCreated ;
205+ window . Destroying -= OnWindowOnDestroying ;
206+ window . Backgrounding -= OnWindowOnBackgrounding ;
207+ window . DisplayDensityChanged -= OnWindowOnDisplayDensityChanged ;
208+
209+ // Navigation events
210+ window . ModalPushed -= OnWindowOnModalPushed ;
211+ window . ModalPopped -= OnWindowOnModalPopped ;
212+ window . PopCanceled -= OnWindowOnPopCanceled ;
213+ }
220214 }
221215
222216 internal void HandleElementEvents ( Element element , bool bind = true )
223217 {
224- // we always unbind the element first to ensure we don't have any sticky or repeat hooks
225- // Rendering events
226- element . ChildAdded -= OnElementOnChildAdded ;
227- element . ChildRemoved -= OnElementOnChildRemoved ;
228- element . ParentChanged -= OnElementOnParentChanged ;
229-
230- // BindableObject events
231- element . BindingContextChanged -= OnElementOnBindingContextChanged ;
232-
233218 if ( bind )
234219 {
235220 // Rendering events
@@ -244,30 +229,34 @@ internal void HandleElementEvents(Element element, bool bind = true)
244229 // BindableObject events
245230 element . BindingContextChanged += OnElementOnBindingContextChanged ;
246231 }
232+ else
233+ {
234+ // Rendering events
235+ element . ChildAdded -= OnElementOnChildAdded ;
236+ element . ChildRemoved -= OnElementOnChildRemoved ;
237+ element . ParentChanged -= OnElementOnParentChanged ;
238+
239+ // BindableObject events
240+ element . BindingContextChanged -= OnElementOnBindingContextChanged ;
241+ }
247242 }
248243
249244 internal void HandleVisualElementEvents ( VisualElement element , bool bind = true )
250245 {
251- element . Focused -= OnElementOnFocused ;
252- element . Unfocused -= OnElementOnUnfocused ;
253-
254246 if ( bind )
255247 {
256248 element . Focused += OnElementOnFocused ;
257249 element . Unfocused += OnElementOnUnfocused ;
258250 }
251+ else
252+ {
253+ element . Focused -= OnElementOnFocused ;
254+ element . Unfocused -= OnElementOnUnfocused ;
255+ }
259256 }
260257
261258 internal void HandleShellEvents ( Shell shell , bool bind = true )
262259 {
263- // Navigation events
264- // https://docs.microsoft.com/dotnet/maui/fundamentals/shell/navigation
265- shell . Navigating -= OnShellOnNavigating ;
266- shell . Navigated -= OnShellOnNavigated ;
267-
268- // A Shell is also a Page
269- HandlePageEvents ( shell , bind : false ) ;
270-
271260 if ( bind )
272261 {
273262 // Navigation events
@@ -278,23 +267,20 @@ internal void HandleShellEvents(Shell shell, bool bind = true)
278267 // A Shell is also a Page
279268 HandlePageEvents ( shell ) ;
280269 }
270+ else
271+ {
272+ // Navigation events
273+ // https://docs.microsoft.com/dotnet/maui/fundamentals/shell/navigation
274+ shell . Navigating -= OnShellOnNavigating ;
275+ shell . Navigated -= OnShellOnNavigated ;
276+
277+ // A Shell is also a Page
278+ HandlePageEvents ( shell , bind : false ) ;
279+ }
281280 }
282281
283282 internal void HandlePageEvents ( Page page , bool bind = true )
284283 {
285- // Lifecycle events
286- // https://docs.microsoft.com/dotnet/maui/fundamentals/shell/lifecycle
287- page . Appearing -= OnPageOnAppearing ;
288- page . Disappearing -= OnPageOnDisappearing ;
289-
290- // Navigation events
291- // https://github.com/dotnet/docs-maui/issues/583
292- page . NavigatedTo -= OnPageOnNavigatedTo ;
293-
294- // Layout changed event
295- // https://docs.microsoft.com/dotnet/api/xamarin.forms.ilayout.layoutchanged
296- page . LayoutChanged -= OnPageOnLayoutChanged ;
297-
298284 if ( bind )
299285 {
300286 // Lifecycle events
@@ -310,6 +296,21 @@ internal void HandlePageEvents(Page page, bool bind = true)
310296 // https://docs.microsoft.com/dotnet/api/xamarin.forms.ilayout.layoutchanged
311297 page . LayoutChanged += OnPageOnLayoutChanged ;
312298 }
299+ else
300+ {
301+ // Lifecycle events
302+ // https://docs.microsoft.com/dotnet/maui/fundamentals/shell/lifecycle
303+ page . Appearing -= OnPageOnAppearing ;
304+ page . Disappearing -= OnPageOnDisappearing ;
305+
306+ // Navigation events
307+ // https://github.com/dotnet/docs-maui/issues/583
308+ page . NavigatedTo -= OnPageOnNavigatedTo ;
309+
310+ // Layout changed event
311+ // https://docs.microsoft.com/dotnet/api/xamarin.forms.ilayout.layoutchanged
312+ page . LayoutChanged -= OnPageOnLayoutChanged ;
313+ }
313314 }
314315
315316 // Application Events
0 commit comments