2011-03-01 196 views
2

我有一個使用微軟提供的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永遠不會引發該事件。 任何人都可以建議爲什麼?

回答

2

如果GPSEmulator正在運行且仍然... GPS狀態:NoData!

試試這個!

GPSEmulator/MainWindow.xaml.cs /線:391

string lat = currentPosition.X.ToString().Replace(",", "."); 
string lon = currentPosition.Y.ToString().Replace(",", "."); 
transmittedLocation = lat+","+lon; 
+0

實際上Watcher狀態ID總是NoData。但是我不明白你在建議寫這個代碼的位置......也許在GPSEmulator的源代碼中?你認爲這是模擬器的問題嗎? – themarcuz 2011-03-03 21:45:12

+0

你有它的人!這是一個GPS模擬器的錯誤!也許這是一個來自區域設置或類似的問題。無論如何,你救了我! – themarcuz 2011-03-03 22:22:37

0

您是否首次在視圖或頁面構造函數中訂閱了OnLoaded事件?

public MainPage() 
    { 
     InitializeComponent(); 
     Loaded += OnLoaded; 
    } 
+0

當然我做到了。對不起,沒那麼具體。發佈編輯 – themarcuz 2011-03-01 09:30:15

+0

好吧,我想我明白了。嘗試在CreateObservableGeoPositionWatcher中啓動Watcher(就在返回可觀察值之前)。在訂閱之前,可觀察對象似乎與Gps Start關聯。 – DavidP 2011-03-01 11:12:52

+0

沒辦法。即使有你的建議,處理程序OnPositionChanged也不會被調用。我到底做錯了什麼? – themarcuz 2011-03-03 20:42:56