我使用微软提供的GPS模拟器时遇到问题。 这是我的代码
public IGeoPositionWatcher<GeoCoordinate> Watcher { get; private set; }
public IObservable<GeoCoordinate> ObservableGeoCoordinate { get; set; }
private void InitializeGpsDevice()
{
try
{
if (Watcher == null)
{
Watcher = new GpsEmulatorClient.GeoCoordinateWatcher();
}
ObservableGeoCoordinate = CreateObservableGeoPositionWatcher();
Watcher.Start();
}
catch (Exception ex)
{
MessageBox.Show(string.Format("Failed to initialize GPS device:{0}", ex.Message), "GPS Error", MessageBoxButton.OK);
}
}
private IObservable<GeoCoordinate> CreateObservableGeoPositionWatcher()
{
var observable = Observable.FromEvent<GeoPositionChangedEventArgs<GeoCoordinate>>(
e => Watcher.PositionChanged += e,
e => Watcher.PositionChanged -= e
).Select(e => e.EventArgs.Position.Location);
return observable;
}
创建我的IObservable对象后,我就这样使用它:
public MainPage()
{
InitializeDefaults();
InitializeComponent();
Loaded += OnLoaded;
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
var observable = GpsHelper.Instance.ObservableGeoCoordinate;
observable
.ObserveOnDispatcher()
.Subscribe(OnPositionChanged);
}
private void OnPositionChanged(GeoCoordinate location)
{
Map.Center = location;
}
但事件OnPositionChanged从未被提出过。 任何人都可以建议为什么?
答案 0 :(得分:2)
如果GPSEmulator正在运行且仍然...... GPS状态:NoData!
试试这个!
GPSEmulator / MainWindow.xaml.cs / line:391
string lat = currentPosition.X.ToString().Replace(",", ".");
string lon = currentPosition.Y.ToString().Replace(",", ".");
transmittedLocation = lat+","+lon;
答案 1 :(得分:0)
您是否首先在View或Page构造函数中订阅了OnLoaded事件?
public MainPage()
{
InitializeComponent();
Loaded += OnLoaded;
}