首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xamarin,使用Xlabs样本中的地理位置

Xamarin,使用Xlabs样本中的地理位置
EN

Stack Overflow用户
提问于 2015-05-05 18:52:26
回答 2查看 2.8K关注 0票数 1

使用Xamarin共享项目。

我试着在我的共享项目中加入Xlabs中的地理定位功能,但是在调用dependencyService时我遇到了问题。

我有一个内容页面,其中有一个按钮,命令如下:Command = new Command(async () => await GetPosition(), () => Geolocator != null)

指挥部导致:

代码语言:javascript
复制
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呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-05-05 20:33:05

XLabs.Platform服务不再自动在Xamarin.Forms.DependencyService中注册(因为更新到2.0)。这是因为消除了Xamarin.Forms对XLabs.Platform的依赖。您可以手动注册Geolocator (来自每个特定于平台的项目):

代码语言:javascript
复制
 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/的更多信息

票数 4
EN

Stack Overflow用户

发布于 2015-05-05 19:29:13

Constructor的第二个参数是一个函数,该函数指示命令是否可以执行,在您的示例中,您“告诉”只在Geolocator is != null时执行命令,但是要在命令中初始化命令,因为Geolocator是空的,所以它永远不会执行。

应该在调用命令之前初始化Geolocator,或者删除执行命令的Geolocator is != null条件。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30060996

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档