Skip to content

Conversation

@jeremy-visionaid
Copy link
Contributor

Both the handler and the content are null on detach

Fixes: #13

Both the handler and the content are null on detach

Fixes: davidortinau#13
_content.KeyUp -= OnKeyUp;
_content.PreviewKeyDown -= OnPreviewKeyDown;
_content.PreviewKeyUp -= OnPreviewKeyUp;
_content = null;
Copy link
Owner

Choose a reason for hiding this comment

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

@jeremy-visionaid why set content to null? I don't think this behavior should be responsible for that. What if the app is just removing the behavior but the content should remain unchanged?

Setting content to null doesn't seem right, so removing that until further clarification so we can at least fix this crasher.
@davidortinau davidortinau requested a review from Copilot May 27, 2025 21:20
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a null-reference crash when detaching KeyboardBehavior by caching the window’s content element and guarding against null before unsubscribing its key events.

  • Introduced a private _content field to hold window.Content
  • Updated OnAttachedTo to assign and subscribe via _content
  • Added a null check in OnDetachedFrom before unsubscribing
Comments suppressed due to low confidence (1)

src/Plugin.Maui.KeyListener/KeyboardBehavior.Windows.cs:14

  • [nitpick] The field name _content is generic; consider renaming it to _windowContent or _platformContent to clarify its purpose in storing the window's root element.
UIElement? _content;

Comment on lines 20 to 25
var window = bindable.Window.Handler.PlatformView as Microsoft.UI.Xaml.Window;

window.Content.KeyDown += OnKeyDown;
window.Content.KeyUp += OnKeyUp;
window.Content.PreviewKeyDown += OnPreviewKeyDown;
window.Content.PreviewKeyUp += OnPreviewKeyUp;

//platformView.KeyDown += OnKeyDown;
//platformView.KeyUp += OnKeyUp;
//platformView.PreviewKeyDown += OnPreviewKeyDown;
//platformView.PreviewKeyUp += OnPreviewKeyUp;
_content = window.Content;
_content.KeyDown += OnKeyDown;
_content.KeyUp += OnKeyUp;
_content.PreviewKeyDown += OnPreviewKeyDown;
_content.PreviewKeyUp += OnPreviewKeyUp;
Copy link

Copilot AI May 27, 2025

Choose a reason for hiding this comment

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

Add a null check for window and window.Content before assigning to _content to avoid a potential NullReferenceException. For example:

if (bindable.Window?.Handler?.PlatformView is Microsoft.UI.Xaml.Window win && win.Content is UIElement content)
{
    _content = content;
    // subscribe events...
}

Copilot uses AI. Check for mistakes.
window.Content.KeyUp -= OnKeyUp;
window.Content.PreviewKeyDown -= OnPreviewKeyDown;
window.Content.PreviewKeyUp -= OnPreviewKeyUp;
if (_content is null)
Copy link

Copilot AI May 27, 2025

Choose a reason for hiding this comment

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

After unsubscribing the events in OnDetachedFrom, consider resetting _content = null; to release the reference and avoid holding onto the UI element longer than necessary.

Copilot uses AI. Check for mistakes.
@jeremy-visionaid
Copy link
Contributor Author

Oh. Haha. Just coming back to this one this morning. I am tired and have a cold so maybe I should just try again on this one... 😅 IIRC In my own solution + app I have behaviours attached to multiple pages, so my approach maybe wasn't reasonable here. I can take another look later if you want. Thanks for merging the other fixes!

@davidortinau
Copy link
Owner

@jeremy-visionaid I've asked Matthew on the MAUI team as well about it, and he says '_content = null' resets the local reference only which seems good. If it's working in your scenario with multiple pages then that's confirmation enough for me. I'll return that and merge it after a quick confirmation here. Thanks for the PRs!

@jeremy-visionaid
Copy link
Contributor Author

Thanks for being so responsive! Yeah, it's just a local reference, but I don't think the assumption that it's only used with a single element is necessarily valid. I've got some other work to do before I can come back to this one. More concerning to me is that this isn't going to work for my app anyway without fixes upstream like for #14, so if you have contacts in the WinUI/Windows App SDK team you could poke then that would be really appreciated!

In the meantime, since I'm in NZ, I'll stage some more changes/fixes ready for your attention tomorrow 😄

@davidortinau
Copy link
Owner

Seems reasonable to go ahead and merge this as-is now.

@davidortinau davidortinau merged commit d7f73f3 into davidortinau:main May 27, 2025
2 checks passed
@jeremy-visionaid
Copy link
Contributor Author

Seems reasonable to go ahead and merge this as-is now.

Yeah, it's at least not worse than it was even if it's not 100% correct. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NRE in KeyboardBehavior.OnDetachedFrom on Windows

2 participants