Xamarin.Mac Bundle

bgergobgergo HUMember ✭✭

Hi everyone,

I want to make a dynamically loadable library (which is not known at compile time) which includes storyboards, images and other resources. I want to discover and load everything during run-time. According to what I've already figured out, I should use bundles, but I can't find how to create one in Xamarin Studio.

Any help would be appreciated,
Gergely

Best Answers

  • ChrisHamonsChrisHamons US Xamurai
    Accepted Answer

    I was able to get your example working with two changes:

    • In Interface Builder, remove the Module line you have when you declare your custom class. Xamarin.Mac does not use Modules in this sense, and is unnecessary \ breaks things.
    • By default NSApplication.Init will use reflection to walk all loaded assemblies and handle their registar\export attributes, but only for loaded assemblies. Since your main application does not have a strong reference to force that assembly loaded, you miss out on this. You have two options:
      • Add something like var v = typeof (ModuleTestLib.MyViewController); before NSApplicaiton.Init (or something else to force the assembly to be loaded) so that we catch it in the automated scan.
      • Do it yourself by calling something like ObjCRuntime.Runtime.RegisterAssembly (typeof (ModuleTestLib.MyViewController).Assembly); AFTER NSApplication.Init and before your try to use your view.

Answers

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    Are you talking about frameworks (https://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/BPFrameworks/Concepts/FrameworkAnatomy.html)?

    We currently don't have built in support for generating them, if this is what you are looking for.

    However, they really are just specially named directories with things in the right locations. You could generate one doing something like this:

    https://developer.xamarin.com/samples/mac/UseMSBuildToCopyFilesToBundleExample/

  • bgergobgergo HUMember ✭✭

    Hi Chris,

    thank you for your fast response. I'm porting a middle-sized Windows app to Mac OS X, so it's possible that I will have a few questions at the beginning, for which I haven't found tutorials...

    Regarding Bundles:
    I think I'm talking about bundles. These would function as plugins in my application. I hope I'll manage to get on with this now.

    Regarding Frameworks:
    I also want to share code between applications, for which - if I'm right - the solution is frameworks. Regarding this, I couldn't prevent a .dll from copying to the application's bundle. I tried to move the appropriate .dll manually to /Library/Frameworks/MyFramework.framework but after this the app crashed. So I'd need a little help in connection with this as well.

    Regarding Custom Control Libraries:
    I also want to make a custom control library. I don't know if it should be a bundle, a library with module output, or what?
    I tried to enter a custom class name in Xcode for a View/ViewController and the name of a referenced library in which the class resides, but it didn't work.

  • DaveHuntDaveHunt USMember ✭✭✭✭✭

    When sharing code between applications with C#/.NET/Xamarin, you use assemblies, not frameworks. Your dll is already an assembly, so there's nothing special to do. Just reference the assembly from your application and you're good to go. It's supposed to get copied to the application bundle, because it's needed by the application.

    Plugins would essentially work the same way. You implement them as assemblies (dlls) but load them dynamically via Assembly.Load().

  • bgergobgergo HUMember ✭✭

    I have a solution with multiple applications. I just wanted to prevent the common assemblies from copying to every application's bundle.

    Assembly.Load() is not enough for me regarding plugins, because my plugins need to contain some UI (images, storyboards) and I can't embed them (especially not storyboards) in a simple .NET assembly.

    Is there a tutorial on how to make custom control libraries?

  • bgergobgergo HUMember ✭✭

    Thank you very much, first two questions answered.

    I read the custom control tutorial you posted. But I'm curious about how to put the controls in a separate library (and use them in Xcode)?

  • DaveHuntDaveHunt USMember ✭✭✭✭✭

    Create the custom control in a class library project. Then you can reference that project or dll in any project that wants to use it.

    As long as you've included the proper Register and Export attributes, Interface Builder should be able to use it. It shouldn't matter that it's in a referenced dll instead of a source file in your project.

  • bgergobgergo HUMember ✭✭

    I don't know what's the problem. This feature seems to be broken on my system. Could you maybe send me a very simple working example, so I can test if I have installation problems, or there is something else.

    I've attached my very simple not working project.

  • DaveHuntDaveHunt USMember ✭✭✭✭✭

    I thought you were creating a custom control. That project only contains a "custom" view controller.

    At any rate, what do you mean by "broken on your system"? What isn't working?

    I'm not running a current version of Xamarin, so I can't very easily provide you with a working project.

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai
    Accepted Answer

    I was able to get your example working with two changes:

    • In Interface Builder, remove the Module line you have when you declare your custom class. Xamarin.Mac does not use Modules in this sense, and is unnecessary \ breaks things.
    • By default NSApplication.Init will use reflection to walk all loaded assemblies and handle their registar\export attributes, but only for loaded assemblies. Since your main application does not have a strong reference to force that assembly loaded, you miss out on this. You have two options:
      • Add something like var v = typeof (ModuleTestLib.MyViewController); before NSApplicaiton.Init (or something else to force the assembly to be loaded) so that we catch it in the automated scan.
      • Do it yourself by calling something like ObjCRuntime.Runtime.RegisterAssembly (typeof (ModuleTestLib.MyViewController).Assembly); AFTER NSApplication.Init and before your try to use your view.
Sign In or Register to comment.