Device.StartTimer Xamarin 后崩溃
Crash after Device.StartTimer Xamarin
我正在 Xamarin 应用程序的页面之间切换。从每 10 秒更新一次的列表,到每 250 毫秒检查一次布尔值的另一种形式。 运行 和 Device.StartTimer,但不应重叠。
这是列表表单上的开始计时器:
protected override void OnAppearing()
{
base.OnAppearing();
vm.cmdReload.Execute(null);
//...
blnTimerActive = true;
startTimer();
}
private void startTimer()
{
Device.StartTimer(System.TimeSpan.FromSeconds(10), () =>
{
if (!blnTimerActive)
return false;
UpdateUserDataAsync();
return true;
}); // This is where it breaks, I think..
}
protected override void OnDisappearing()
{
base.OnDisappearing();
blnTimerActive = false;
}
每次我尝试 运行 时,它都会显示一条错误消息“线程上没有兼容的代码 运行ning”和“对象引用未设置为对象的实例”。
Error message
这发生在我进入下一张表格并且这张表格“消失”之后我不确定为什么这个计时器仍然 运行ning。任何帮助深表感谢!如果我能让这个问题更容易理解,请告诉我。
我通过仔细检查输出发现了它。它在我的代码中崩溃了,但不知何故没有显示中断发生的位置。我检查了输出,它准确地显示了它的位置。
If this is happening to you, check your output carefully. It should
say what line number there was an error in, add a try catch, and step
through. You should find your error easily that way.
感谢大家的帮助!
我正在 Xamarin 应用程序的页面之间切换。从每 10 秒更新一次的列表,到每 250 毫秒检查一次布尔值的另一种形式。 运行 和 Device.StartTimer,但不应重叠。
这是列表表单上的开始计时器:
protected override void OnAppearing()
{
base.OnAppearing();
vm.cmdReload.Execute(null);
//...
blnTimerActive = true;
startTimer();
}
private void startTimer()
{
Device.StartTimer(System.TimeSpan.FromSeconds(10), () =>
{
if (!blnTimerActive)
return false;
UpdateUserDataAsync();
return true;
}); // This is where it breaks, I think..
}
protected override void OnDisappearing()
{
base.OnDisappearing();
blnTimerActive = false;
}
每次我尝试 运行 时,它都会显示一条错误消息“线程上没有兼容的代码 运行ning”和“对象引用未设置为对象的实例”。 Error message
这发生在我进入下一张表格并且这张表格“消失”之后我不确定为什么这个计时器仍然 运行ning。任何帮助深表感谢!如果我能让这个问题更容易理解,请告诉我。
我通过仔细检查输出发现了它。它在我的代码中崩溃了,但不知何故没有显示中断发生的位置。我检查了输出,它准确地显示了它的位置。
If this is happening to you, check your output carefully. It should say what line number there was an error in, add a try catch, and step through. You should find your error easily that way.
感谢大家的帮助!