我正在尝试为Android安装MvxGeolocation插件,但是我遇到了让观察者解雇成功事件的问题。它只是不会开火。
我已按照MvvmCross
(https://github.com/MvvmCross/MvvmCross/wiki/MvvmCross-plugins#location)的文档进行操作。
根据文档,我创建了一个单独的服务,用于向ViewModel
发布消息,而不是将IMvxLocationWatcher
直接挂钩到ViewModel
。
public class GeoLocationService: IGeoLocationService
{
private readonly IMvxLocationWatcher _watcher;
private readonly IMvxMessenger _messenger;
public GeoLocationService(IMvxLocationWatcher watcher, IMvxMessenger messenger)
{
_watcher = watcher;
_messenger = messenger;
_watcher.Start(new MvxLocationOptions(), OnLocation, OnError);
}
public void OnLocation(MvxGeoLocation location)
{
var message = new LocationMessage(this, location.Coordinates.Latitude, location.Coordinates.Longitude);
_messenger.Publish(message);
}
public void OnError(MvxLocationError error)
{
Mvx.Error("Seen location error {0}", error.Code);
}
}
OnLocation(MvxGeoLocation location)
方法用于发布我的ViewModel订阅的消息,如下所示: -
public class MainViewModel : MvxViewModel
{
private readonly MvxSubscriptionToken _token;
public MainViewModel(IMvxMessenger messenger)
{
_token = messenger.SubscribeOnMainThread<LocationMessage>(OnLocationMessage);
}
private void OnLocationMessage(LocationMessage locationMessage)
{
Lat = locationMessage.Lat;
Lng = locationMessage.Lng;
}
double _latitude;
public double Lat
{
get
{
return _latitude;
}
set
{
_latitude = value;
RaisePropertyChanged(() => Lat);
}
}
double _longitude;
public double Lng
{
get
{
return _longitude;
}
set
{
_longitude = value;
RaisePropertyChanged(() => Lng);
}
}
string _name;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
RaisePropertyChanged(() => Name);
}
}
}
我已经在Android应用程序中添加了Bootstrapper类来解析Messenger和LocationWatcher插件。我也在我的核心库中的App类中注册了GeoLocationService,如下所示: -
public class App : MvxApplication
{
public App()
{
Mvx.ConstructAndRegisterSingleton<IGeoLocationService, GeoLocationService>();
Mvx.RegisterSingleton<IMvxAppStart>(new MvxAppStart<MainViewModel>());
}
}
我还编辑了AndroidManifest.Xml并添加了以下用户权限: -
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
我正在针对插入的设备(Samsung S6 Edge)进行调试。
我已经测试了我的view.xaml,以确保绑定工作时传递一些默认值..并且一切都按预期进行。
问题是,从不调用Watcher类中的成功或错误的动作。