dotnet / dotnet/macios

Apple change causing "Event registration is overwriting existing delegate" exception to fire when using C# events of NSWindowDelegate

Open
#6,898 6 comments 0 reactions 0 assignees View on GitHub
bug iOS macOS
Dominant language
C#
Stars
2.9k
Forks
576
Avg merge
2d 13h
Merged PRs (30d)
96

Description

Original bug: https://forums.xamarin.com/discussion/comment/387768/#Comment_387768

```public override void ViewWillAppear() { base.ViewWillAppear(); View.Window.WindowShouldClose += Window_WindowShouldClose; }```

Exception:
```Event registration is overwriting existing delegate. Either just use events or your own delegate: TestApp.WindowController AppKit.NSWindow+_NSWindowDelegate```

It appears that Cocoa in some update is assigning a default delegate on Windows, and the exception is warning/complaining that we're overwriting it blindly. From what I can tell, this is a behavior change from Apple.

You can either:

Stop using the events and assign a View.Window.Delegate directly
OR
Set `View.Window.WeakDelegate = null` before assigning your first event
OR
If you want to completely disable the safety set `NSApplication.CheckForEventAndDelegateMismatches` to `false`

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.