locked
Type registrar and dynamically (at runtime) loaded libraries RRS feed

  • Question

  • User8299 posted

    Hi there,

    I'm still trying to get a basic plugin system set up in Xamarin.Mac. Basically I want to be able to load a bundle containing a compiled mono library and additional assets like views, windows, images, etc. at runtime.

    The basics are working. I create my .framework bundle as part of a post-build script, copy over any resources I need and am able to dynamically load that bundle at runtime, instantiate classes that are contained within it and load Cocoa windows and views. So far so good.

    However, I'm now having an issue with the type registrar not surfacing some of my classes to the Objective-C runtime. The classes that don't get registered with the Objective-C runtime are not instantiated by my code but through xib's. For example, I have a xib with a window for which I set the subclass to one of the classes in the dynamically loaded dll.

    When running the application I see Unknown Window class DragBackgroundWindow in Interface Builder file, creating generic Window instead in the output window and if I try this code IntPtr classHandle = ObjCRuntime.Class.GetHandle("DragBackgroundWindow");, the classHandle is a zero pointer.

    Now if I instantiate this class once before the xib actually loads, I have non of these issues which leads me to believe that this forces the runtime to register the class with the Objective-C runtime. That's not really a good workaround however as it obviously creates unnecessary garbage/dummy objects and isn't very intuitive either.

    So the question boils down to: How on earth can I let the Xamarin.Mac runtime know that I want it to register all subclasses of Cocoa types to the Objective-C runtime upon loading a dll?

    Here's my current work in progress sample: https://github.com/lemonmojo/xamarin-mac-plugin-test Changing if (false) { to if (true) { in line 37 of UIStuffMainClass.cs of the UIStuff project works around the problem but isn't the solution I'm looking for.

    thx, felix

    Monday, April 10, 2017 6:13 PM

Answers

  • User35201 posted

    I have not yet looked at your sample, but I believe I understand your problem:

    So the question boils down to: How on earth can I let the Xamarin.Mac runtime know that I want it to register all subclasses of Cocoa types to the Objective-C runtime upon loading a dll?

    Try: ObjCRuntime.Runtime.RegisterAssembly (Assembly a) on the assembly after dynamically loading it.

    When you call NSApplication.Init, we scan all loaded assemblies for registration: https://github.com/xamarin/xamarin-macios/blob/master/src/AppKit/NSApplication.cs#L59

    If you dynamically load things later, that's the API for letting our runtime know there is more work to be done.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, April 10, 2017 6:31 PM

All replies

  • User35201 posted

    I have not yet looked at your sample, but I believe I understand your problem:

    So the question boils down to: How on earth can I let the Xamarin.Mac runtime know that I want it to register all subclasses of Cocoa types to the Objective-C runtime upon loading a dll?

    Try: ObjCRuntime.Runtime.RegisterAssembly (Assembly a) on the assembly after dynamically loading it.

    When you call NSApplication.Init, we scan all loaded assemblies for registration: https://github.com/xamarin/xamarin-macios/blob/master/src/AppKit/NSApplication.cs#L59

    If you dynamically load things later, that's the API for letting our runtime know there is more work to be done.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, April 10, 2017 6:31 PM
  • User8299 posted

    Perfect! That did it!

    Many thx, felix

    Monday, April 10, 2017 6:54 PM