2016-08-22 25 views
0

我開發的應用程序在Xamarin,這是在手機上跟蹤用戶的當前位置。如何調試不幸MyApp已停止?工程模擬器上,而不是手機

當我在它的工作原理沒有問題的模擬器,當我在手機上的應用程序部署工作10秒,我可以看到GPS圖標閃爍頂級測試應用程序。 5秒後,GPS圖標停止閃爍,應用程序崩潰,「不幸MyApp已停止」。

我怎麼能調試這在手機上,因爲它適用於模擬器?我認爲這是一些線程問題。

得到堆棧跟蹤,SIGSEGV致命錯誤: http://pastebin.com/Bxt68ikj

我的代碼:
Timer.cs

public delegate void TimerCallback(object state); 

    public sealed class Timer : CancellationTokenSource, IDisposable 
    { 
     public int DueTime { get; set; } 
     public int Period { get; set; } 

     public void Start(TimerCallback callback, object state) 
     { 
      Task.Delay(DueTime, Token).ContinueWith(async (t, s) => 
      { 
       var tuple = (Tuple<TimerCallback, object>)s; 

       while (true) 
       { 
        if (IsCancellationRequested) 
         break; 
        Task.Run(() => tuple.Item1(tuple.Item2)); 
        await Task.Delay(Period); 
       } 

      }, Tuple.Create(callback, state), CancellationToken.None, 
       TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnRanToCompletion, 
       TaskScheduler.Default); 
     } 

     public new void Dispose() { base.Cancel(); } 
    } 

TimerLocationService.cs

public class TimerLocationService : ITimerLocationService 
{ 
    private Timer _timer; 

    public void Start(TimerCallback callback, object state) 
    { 
     _timer = new Timer(); 
     _timer.DueTime = 60000; 
     _timer.Period = 60000; 
     _timer.Start(callback, null); 
    } 

    public void SetTimer(int dueTime, int period) 
    { 
     _timer.DueTime = dueTime; 
     _timer.Period = period; 
    } 

    public void Dispose() 
    { 
     _timer.Dispose(); 
    } 
} 

MainPageViewModel.cs

public class MainPageViewModel : ViewModelBase 
    { 
     private readonly IAccountService _accountService; 
     private readonly IGeolocatorService _geolocatorService; 
     private readonly ITimerLocationService _timerLocationService; 

     public MainPageViewModel(IAccountService accountService, IGeolocatorService geolocatorService, ITimerLocationService timerLocationService) 
     { 
      _accountService = accountService; 
      _geolocatorService = geolocatorService; 
      _timerLocationService = timerLocationService; 
      LabelUsername = GetUsername(); 
      GetCurrentCoordinates(); 
      _timerLocationService.Start(SendCoordinates, null); 
     } 

     private string _logoutButtonText = "Login"; 

     public string LogoutButtonText 
     { 
      get { return _logoutButtonText; } 
      set 
      { 
       _logoutButtonText = value; 
       RaisePropertyChanged(); 
      } 
     } 

     private string _labelUsername = "Login"; 

     public string LabelUsername 
     { 
      get { return _labelUsername; } 
      set 
      { 
       _labelUsername = value; 
       RaisePropertyChanged(); 
      } 
     } 

     private string _labelCoordinate = "0"; 
     public string LabelCoordinate 
     { 
      get { return _labelCoordinate; } 
      set 
      { 
       _labelCoordinate = value; 
       RaisePropertyChanged(); 
      } 
     } 

     public ICommand LogoutCommand => new MethodInvokerCommand(Logout); 
     public ICommand TimerCommand => new MethodInvokerCommand(SetTimer); 

     private void SetTimer() 
     { 
      _timerLocationService.SetTimer(10000, 10000); 
     } 

     private void Logout() 
     { 
      _accountService.Logout(); 
     } 

     private string GetUsername() 
     { 
      return _accountService.GetUsername(); 
     } 

     private async void GetCurrentCoordinates() 
     { 
      var position = await _geolocatorService.GetPositionAsync(); 

      LabelCoordinate = position.Latitude + ", " + position.Longitude; 

      await _geolocatorService.SendPositionAsync(); 
     } 

     private async void SendCoordinates(object args) 
     { 
      await _geolocatorService.SendPositionAsync(); 
     } 
    } 
+0

您可以通過把一些的try/catch結構在那裏開始,只是彈出一個消息框,以針點它發生在哪裏 –

+0

我加堆棧跟蹤,同時通過USB連接裝置和使用調試。在調試模式下甚至不能在手機上啓動。 –

+0

'無法加載文件或程序集「System.Runtime,版本= 4.0.0.0,文化=中性公鑰= b03f5f7f11d50a3a」或其dependencies.'它說,它缺少一些文件(S)中的一個? –

回答

0

爲什麼你有一個計時器......呢?

您可以訂閱事件時引發的位置變化,只是相應的措施。通過這樣的定時器輪詢是非常1990年代的。

+0

我不想在發生位置更改時發送,這將會每秒發送一次。無論位置如何,我都想每隔1分鐘發一次。 –

相關問題