问题描述
使用Xamarin共享项目.
Using Xamarin Shared Project.
我试图在我的共享项目中包括Xlabs示例中的Geolocation功能,但是在调用dependencyService时遇到了问题.
I tryed to include in my share project the Geolocation feature from Xlabs sample but i'm having problems when it comes to call the dependencyService.
我有一个内容页面,在那儿有一个带有如下命令的按钮:Command = new Command(async () => await GetPosition(), () => Geolocator != null)
I have a content page and in there i have my button that has a command like this: Command = new Command(async () => await GetPosition(), () => Geolocator != null)
命令导致:
private IGeolocator Geolocator
{
get
{
if (_geolocator == null)
{
_geolocator = DependencyService.Get<IGeolocator>();
_geolocator.PositionError += OnListeningError;
_geolocator.PositionChanged += OnPositionChanged;
}
return _geolocator;
}
}
,当它应该调用_geolocator = DependencyService.Get<IGeolocator>();
时,什么也没有发生,并且_geolocator保持为空.
and when it should call the _geolocator = DependencyService.Get<IGeolocator>();
nothing happens and _geolocator remains null.
我有来自Xlabs的所有引用,并且IGeolocator接口在我的项目中,所以为什么不调用DependencyService?
I have all the references from Xlabs and the interface IGeolocator is in my project so why isn't the DependencyService being called??
推荐答案
XLabs.Platform服务不再(自更新至2.0)不再自动向Xamarin.Forms.DependencyService注册.这是由于删除了XLabs.Platform上的Xamarin.Forms依赖关系.您可以手动注册Geolocator(从每个平台特定的项目中注册):
XLabs.Platform services are no longer (since update to 2.0) automatically registered with Xamarin.Forms.DependencyService. This is due to removal of Xamarin.Forms dependency on XLabs.Platform. You can either register the Geolocator manually (from each platform specific project):
DependencyService.Register<Geolocator> ();
更好的选择是使用XLabs.IoC提供的IoC容器抽象(作为依赖项自动包含在内): https://github.com/XLabs/Xamarin-Forms-Labs/wiki/IOC
Better option would be to use the IoC container abstraction provided by XLabs.IoC (included automatically as dependency): https://github.com/XLabs/Xamarin-Forms-Labs/wiki/IOC
有关XLabs.IoC的更多信息: http ://www.codenutz.com/autofac-ninject-tinyioc-unity-with-xamarin-forms-labs-xlabs/
More on XLabs.IoC: http://www.codenutz.com/autofac-ninject-tinyioc-unity-with-xamarin-forms-labs-xlabs/
这篇关于Xamarin,使用Xlabs示例中的Geolocation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!