我有一個使用微軟提供的GPS模擬器的問題。 這是我的代碼WP7 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永遠不會引發該事件。 任何人都可以建議爲什麼?
實際上Watcher狀態ID總是NoData。但是我不明白你在建議寫這個代碼的位置......也許在GPSEmulator的源代碼中?你認爲這是模擬器的問題嗎? – themarcuz 2011-03-03 21:45:12
你有它的人!這是一個GPS模擬器的錯誤!也許這是一個來自區域設置或類似的問題。無論如何,你救了我! – themarcuz 2011-03-03 22:22:37