简体   繁体   English

Xamarin.Forms自定义控件库

[英]Xamarin.Forms custom control library

How can i create custom control library based on Xamrin.Forms with my custom controls and platform specified code ? 如何使用自定义控件和平台指定的代码基于Xamrin.Forms创建自定义控件库? I need to make module architecture with posibility to add some modules with platform specified code. 我需要使模块体系结构具有可能性,以使用平台指定的代码添加一些模块。

Everything you're describing is already accounted for in the Xamarin.Forms framework. 您描述的所有内容都已经在Xamarin.Forms框架中说明了。 Specifically, there is support for UI controls with platform-specific renderers and for non-ui code using the DependencyService. 具体来说,它支持使用特定于平台的渲染器的UI控件以及使用DependencyService的非UI代码。 In both cases, you define common data structures, interfaces and View subclasses in a common library that references Xamarin.Forms and targets the Android/iOS/Windows Phone platforms, and provide platform-specific code in assemblies that specifically target the various platforms. 在这两种情况下,您都可以在引用Xamarin.Forms并以Android / iOS / Windows Phone平台为目标的公共库中定义公共数据结构,接口和View子类,并在专门针对各种平台的程序集中提供平台特定的代码。

In the case of the UI controls, you create platform-specific renderers (aka "Custom Renderers") and register them with the Xamarin Forms runtime using the ExportRenderer attribute. 对于UI控件,您可以创建特定于平台的渲染器(也称为“自定义渲染器”),并使用ExportRenderer属性在Xamarin Forms运行时中注册它们。 In practice, the way this should work is that the control from the "API assembly" (from here on, I'll use the term Jason Smith uses for it: the "model") is used to capture any configuration or data binding, and the custom renderer then uses the data contained in the model to determine how the native controls looks/behaves. 在实践中,这种工作方式应该是“ API程序集”中的控件(从这里开始,我将使用Jason Smith所用的术语:“模型”)来捕获任何配置或数据绑定,然后自定义渲染器使用模型中包含的数据来确定本机控件的外观/行为。 It's important to note that there are effectively two instances here: the model instance, which is what the Xamarin Forms view is using as an API surface to control the actual control's behavior, and the renderer instance, which is what's actually drawing the UI. 重要的是要注意,这里实际上有两个实例:模型实例(这是Xamarin Forms视图用作控制实际控件行为的API表面),以及渲染器实例(实际上是在绘制UI)。 If there is any data binding, you'll want the renderer to watch the model's bindable properties for changes so the UI can reflect the state of the model. 如果存在任何数据绑定,则需要渲染器监视模型的可绑定属性以进行更改,以便UI可以反映模型的状态。 This topic is described in much more detail in Xamarin's docs for Custom Renderers . Xamarin的Custom Renderers文档中对该主题进行了更详细的描述。

In the case of non-UI code ("headless" functionality such as geolocation, sensors, etc.) you can use the DependencyService. 对于非UI代码(“无头”功能,如地理位置,传感器等),可以使用DependencyService。 Similar to the UI controls, you define an abstract API (expressed as interfaces) that exists in the "API Assembly" to provide a platform-independent way to access the functionality and you provide an implementation for each supported platform that is registered using the DependencyService attribute. 与UI控件类似,您定义“ API程序集”中存在的抽象API(表示为接口),以提供独立于平台的方式来访问功能,并为每个使用DependencyService注册的受支持平台提供一个实现。属性。 In practice, you can think of the DependencyService as a Dependency Injection container that is automatically populated using that attribute. 实际上,您可以将DependencyService视为使用该属性自动填充的Dependency Injection容器。 Again, this topic is fully covered in the Xamarin docs for the DependencyService . 同样, Xamarin文档中DependencyService完全涵盖了该主题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM