我正在Xamarin开发应用程序,它正在跟踪手机上用户的当前位置。
当我在模拟器上测试应用程序时它没有任何问题,当我在手机应用程序上部署它工作10秒钟时,我可以看到顶部有GPS图标闪烁。 5秒钟后,GPS图标停止闪烁,应用程序崩溃与#34;不幸的是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 :(得分:0)
为什么你有一个计时器......一点都没有?
您可以订阅位置更改时引发的事件,并对此作出反应。通过这样的计时器进行轮询就是1990年代。