xamarin.net-6.0.net-8.0xamarin.mac.net-mac

AppKit.NsWindow error on .NetMAC OS application after migration from Xamarin.MAC


I have Xamarin.MAC native application built with C# language. Since the Xamarin support is stopped, I have to migrate it to .Net MAC OS. But I am not seeing the AppKit.Window in the .Net MAC OS. In Xamarin , I can see an option to create window controller, but in .net MAC OS, I can see only option to create View and view controller. Do you know why? How Can I convert my existing window and window controllers? I have copied my existing window and window controller to .Net MAC Os project, but when I open the window controller using the below code, I am getting the following error. Do you know why and how can I solve this issue?

homeWindowController = new HomeWindowController ();
CommonClass.HomeController = homeWindowController;
homeWindowController.Window.MakeKeyAndOrderFront (this);

Following are the error details.

Argue-4000"Objective-C exception thrown. Name: NSInternalInconsistencyException Reason: initWithWindowRef: is not currently supported for NSWindow subclasses!\nNative stack trace:\n\t0 CoreFoundation 0x000000018477c570 __exceptionPreprocess + 176\n\t1 libobjc.A.dylib 0x000000018426deb4 objc_exception_throw + 60\n\t2 CoreFoundation 0x000000018477c460 +[NSException exceptionWithName:reason:userInfo:] + 0\n\t3 AppKit 0x0000000188991398 -[NSWindow(NSCarbonExtensions) initWithWindowRef:]


Solution

  • Try to change the constructors of your window controler and window (and any other NSObject-inherited classes) to take a NativeHandle instead of an IntPtr, e.g.

    Previously, your MyWindow(IntPtr) constructor would call the NSWindow(NativeHandle) constructor (because there's an implicit conversion from IntPtr to NativeHandle). During Xamarin -> net6 migration, the separate NSWindow(IntPtr) constructor was added (to bind the initWithWindowRef: selector), and now your Window constructor suddenly calls the NSWindow(IntPtr) constructor instead of the NSWindow(NativeHandle) constructor (and this doesn't work).

    This is why the fix should work: your code will call the correct NSWindow(NativeHandle) constructor again.

    P.S. The compiler should show you a corresponding warnings about changing the constructors signature (and a lot of other warnings too). So, it is good idea to check the new warnings after the migration and address them.