Xamarin Mac 注册机构Xamarin.Mac registrar

本文档介绍了 Xamarin 和其他使用配置的用途。This document describes the purpose of the Xamarin.Mac registrar and its different usage configurations.


Xamarin 可以在托管(.NET)和 Cocoa 的运行时之间实现桥梁,使托管类可以调用非托管的目标 C 类,并在发生事件时回调。Xamarin.Mac bridges the gap between the managed (.NET) world and Cocoa's runtime, allowing managed classes to call unmanaged Objective-C classes and be called back when events occur. 执行此 "神奇" 所需的工作由注册机构处理,一般情况下,在视图中隐藏。The work required to preform this “magic” is handled by the registrar and is, in general, hidden from view.

此注册有性能影响,特别是在应用程序启动时,以及了解 "在幕后" 这一情况下所发生的事情有时会很有帮助。There are performance implications of this registration, specifically on application start up time, and understanding a bit of what's going on "under the hood" can sometimes be helpful.


从根本上讲,在启动时注册机构的作业可以分为两个类别:Fundamentally the registrar’s job at startup can be separated into two catagories:

  • 扫描从 NSObject 派生的每个托管类,并收集要公开给目标 C 运行时的项的列表。Scan every managed class for those deriving from NSObject and collect a list of items to be exposed to the Objective-C runtime.
  • 将此信息注册到目标 C 运行时。Register this information with the Objective-C runtime.

随着时间的推移,将创建三个不同的注册器配置来涵盖不同用例。Over time, three different registrar configurations have been created to cover different use cases. 每个都有不同的生成和运行时后果:Each has different build and run time consequences:

  • 动态注册器–在启动过程中,使用 .net 反射扫描每个加载的类型,确定相关项的列表,并通知本机运行时。Dynamic registrar – During startup, use .NET reflection to scan every loaded type, determine the list of relevant items, and inform the native runtime. 此选项向生成添加了零时间,但在启动期间计算成本非常高(最多秒)。This option adds zero time to the build but is very expensive to compute during launch (up to multiple seconds).
  • 静态注册器–在生成过程中,计算要注册的项集,并生成目标-C 代码来处理注册。Static registrar – During build, compute the set of items to be registered and generate Objective-C code to handle registration. 此代码在启动过程中调用以快速注册所有项。This code is invoked during startup to quickly register all items. 添加了一个很长的暂停时间,但可以在应用程序启动时缩短很长时间。Adds a significant pause to build but can cut a significant amount of time from application start.
  • "部分" 静态-一种较新的 "混合" 方法,这两种方法的优点非常高。"Partial" static – A newer "hybrid" approach which brings most of the advantages of both. 由于Xamarin中的导出是常量,因此请保存预计算库来处理其注册,并链接到中的链接。Since the exports from Xamarin.Mac.dll are constant, save a precomputed library to handle their registration and link that in. 使用反射来处理用户库,但由于用户库导出的类型的类型更少,平台绑定的类型通常相当简单。Use reflection to handle user libraries, but as user libraries export much fewer types that the platform bindings this is often rather quick. Neglectable 的生成时间会受到影响,并减少众多动态的 "成本"。A neglectable build time impact and reduces a vast majority of the “cost” of dynamic.

"今天的部分静态" 是 "调试" 配置的默认值,"静态" 是发布配置的默认设置。Today partial static is the default for Debug configuration and Static is the default for Release configurations.

存在以下情况:There are some scenarios:

  • 启动后加载的、从 NSObject 派生的类的插件Plugins loaded after launch with classes deriving from NSObject
  • 动态创建的类实例,派生自 NSObjectDynamically created class instances deriving from NSObject

注册器无法知道在开始时需要注册某种类型。where the registrar is unable to know that it needs to register some type at start. 提供 ObjCRuntime.Runtime.RegisterAssembly 方法,以通知注册机构有其他要考虑的类型。The ObjCRuntime.Runtime.RegisterAssembly method is provided to inform the registrar that it has additional types to consider.