MVVMCross IMvxGeoLocationWatcher成功动作永远不会在Android上触发

时间:2013-07-17 15:05:19

标签: xamarin.android mvvmcross xamarin-studio

我创建了一个非常简单的测试应用程序,尝试将当前的lat / long地理编码为地址。

以下是我的ViewModel的代码:

namespace LoginProductsMVVM.Core.ViewModels
{
    public class ProductDetailViewModel
        : MvxViewModel
    {
        public void Init(Product product)
        {
            Product = product;
        }

        private Product _product;
        public Product Product
        {
            get { return _product; }
            set { _product = value;
                RaisePropertyChanged (() => Product); }
        }

        private string _latitude;
        public string Latitude{
            get { return _latitude; }
            set { _latitude = value; RaisePropertyChanged(() => Latitude); }
        }

        private string _longitude;
        public string Longitude{
            get { return _longitude; }
            set { _longitude = value; RaisePropertyChanged(() => Longitude); }
        }

        private string _address;
        public string Address{
            get { return _address; }
            set { _address = value; RaisePropertyChanged(() => Address); }
        }

        private IMvxGeoLocationWatcher _watcher;
        public IMvxGeoLocationWatcher Watcher
        {
            get 
            {
                _watcher = Mvx.Resolve<IMvxGeoLocationWatcher> ();
                return _watcher;
            }
        }

        public ProductDetailViewModel(IMvxGeoLocationWatcher watcher)
        {
            _watcher = watcher;
            _watcher.Start (new MvxGeoLocationOptions (), OnLocation, OnError);
        }

        void OnLocation (MvxGeoLocation location)
        {
            Latitude = location.Coordinates.Latitude.ToString();
            Longitude = location.Coordinates.Longitude.ToString();

            // Android Location specific stuff
            var activity = Mvx.Resolve<IMvxAndroidCurrentTopActivity> ().Activity;
            Geocoder geocdr = new Geocoder (activity.BaseContext);

            IList<Address> addresses = geocdr.GetFromLocation (double.Parse(Latitude), double.Parse(Longitude), 1);

            addresses.ToList().ForEach ((addr) => Address += addr.ToString() + "\r\n\r\n");
        }

        void OnError (MvxLocationError error)
        {
            Mvx.Error ("Seen location error {0}", error);
        }
    }
}

我的OnLocation方法有一个断点但它永远不会进入那里。我错过了一些在Android上正常工作的东西吗?它似乎适用于iOS ...

2 个答案:

答案 0 :(得分:1)

Per Odahan here:

  

嗯......我调查了一下:问题已知并且可以在许多设备上看到。这不是MvvmCross问题。简短的回答:设备需要重新启动,所有工作都像魅力一样...似乎谷歌发送了一些导致问题的更新。   这是一个关于这个问题的线程,以及一个关于GeoCode类的类似问题:

     

https://code.google.com/p/android/issues/detail?id=38009

     

所以:可以关闭,MvvmCross还可以,但其他人可以面对这个bug,所以我的解释和链接在这里。

答案 1 :(得分:0)

当你说它永远不会取得“成功”时,实际上是否会成功?

GPS代码可能存在许多问题 - 例如您的应用可能没有权限,您的手机可能已禁用A-GPS或GPS,或者您甚至可能在无位置模拟器上运行 - 所有这可以从您的描述中获得。

值得注意的是,Xamarin.Android在击中断点方面存在长期问题 - 所以添加跟踪比依靠断点命中更好:/


或许尝试从http://mvvmcross.wordpress.com/开始跑N = 8 - 这有帮助吗? (N = 9也值得关注,因为它显示了一种允许多个视图模型使用geowatcher的方法)

相关问题