简体   繁体   English

Xamarin.Forms Prism中特定于平台的代码

[英]Platform specific code in Xamarin.Forms Prism

I have a custom control and I need to use platform specific code inside it. 我有一个自定义控件,我需要在其中使用平台特定的代码。 I register platform specific implementation in IPlatformInitializer s on each platform. 我在每个平台的IPlatformInitializer注册平台特定的实现。 And I'm able to resolve instance via IUnityContainer . 而且我能够通过IUnityContainer解析实例。 But I haven't reference to unity container inside my custom control. 但是我没有在自定义控件中引用统一容器。 What is the designed approach by framework developers to resolve registered instance inside my custom control? 框架开发人员在我的自定义控件中解析注册实例的设计方法是什么? I create my control in XAML . 我在XAML创建控件。

With Xamarin.Forms 's approach I'm able to do: 使用Xamarin.Forms的方法,我可以做到:

DependencyService.Get<ISomeManager>();

from any place of an application. 从应用程序的任何位置。

Thanks in advance. 提前致谢。

You will need to implement a service locator pattern in order to access the container in your custom control. 您将需要实现一个服务定位器模式,以便在您的自定义控件中访问该容器。 You can cast the Application.Current to a PrismApplication and use the Container property off of it, or expose a new static property for the container. 您可以将Application.Current强制转换为PrismApplication并使用其Container属性,或为容器公开一个新的静态属性。

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

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