dotnet / dotnet/macios

InvalidOperationException can occur when using C# events for delegate if Apple sets a default delegate

Open
#7,569 4 comments 0 reactions 0 assignees View on GitHub
documentation macOS
Dominant language
C#
Stars
2.9k
Forks
576
Avg merge
2d 13h
Merged PRs (30d)
96

Description

We have seen a number of “regressions”, for example from https://github.com/xamarin/mac-samples/issues/123 in MacWindows sample:

```
public override void WindowDidLoad ()
{
base.WindowDidLoad ();

// adding Window.WeakDelegate = null here "resolves" the issue
Window.DidResize += (sender, e) => {
// Do something as the window is being live resized
};

Window.DidEndLiveResize += (sender, e) => {
// Do something after the user's finished resizing
// the window
};
}
```

The `Window.DidResize` event throws since Apple started assigning a default delegate they didn’t used to.

You have three options:

- (Preferred) Stop using C# events and create your own delegate and assign that
- Null out the existing delegate with `Window.WeakDelegate = null;`
- Disable the safely check with `NSApplication.CheckForEventAndDelegateMismatches = 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.