使用Xamarin共享项目。
我试着在我的共享项目中加入Xlabs中的地理定位功能,但是在调用dependencyService时我遇到了问题。
我有一个内容页面,其中有一个按钮,命令如下: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仍然是空的。
我有来自Xlabs的所有引用,接口IGeolocator在我的项目中,那么为什么不调用DependencyService呢?
发布于 2015-05-05 20:33:05
XLabs.Platform服务不再自动在Xamarin.Forms.DependencyService中注册(因为更新到2.0)。这是因为消除了Xamarin.Forms对XLabs.Platform的依赖。您可以手动注册Geolocator (来自每个特定于平台的项目):
DependencyService.Register<Geolocator> ();更好的选择是使用由IoC提供的XLabs.IoC容器抽象(自动包含为依赖项):https://github.com/XLabs/Xamarin-Forms-Labs/wiki/IOC
关于XLabs.IoC:http://www.codenutz.com/autofac-ninject-tinyioc-unity-with-xamarin-forms-labs-xlabs/的更多信息
发布于 2015-05-05 19:29:13
Constructor的第二个参数是一个函数,该函数指示命令是否可以执行,在您的示例中,您“告诉”只在Geolocator is != null时执行命令,但是要在命令中初始化命令,因为Geolocator是空的,所以它永远不会执行。
应该在调用命令之前初始化Geolocator,或者删除执行命令的Geolocator is != null条件。
https://stackoverflow.com/questions/30060996
复制相似问题