0

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:]

1
  • The Windows controller is still there for the net8.0-macos workload and works fine. Can you please provide more details about your code?
    – Serg
    Commented Jul 4 at 21:45

1 Answer 1

0

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.

  • from the MyWindowController(IntPtr handle) to the MyWindowController(NativeHandle handle).
  • from the MyWindow(IntPtr handle) to the MyWindow(NativeHandle handle).

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.