Xamarin-macios: InvalidOperationException can occur when using C# events for delegate if Apple sets a default delegate

Created on 11 Dec 2019  ·  4Comments  ·  Source: xamarin/xamarin-macios

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;
documentation macOS

Most helpful comment

FYI @chamons

All 4 comments

FYI @chamons

中国人表示看不懂

呵呵

嘤嘤嘤

Welcome @seattle-a @Gidea-code @dbudaiya - I'm unsure if you are discussing the issue at hand or something unrelated, automated translation is sometimes inaccurate.

As noted here:
https://github.com/xamarin/xamarin-macios/wiki/How-to-Contribute#discussion-etiquette

Whenever possible, we ask that conversations on issues be done in English. We have a global community, and that lets the most people participate.

Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sharmashiv picture sharmashiv  ·  4Comments

rolfbjarne picture rolfbjarne  ·  4Comments

juepiezhongren picture juepiezhongren  ·  3Comments

jzeferino picture jzeferino  ·  3Comments

parmjitv picture parmjitv  ·  4Comments