如何在 Xamarin 中的 UserInterface 线程上实现 Timer Elapsed 事件 Android
How to implement Timer Elapsed Event on UserInterface thread in Xamarin Android
我正在 Xamarin 上实现系统计时器 Android,但我遇到了一个问题,即当倒计时结束时,经过的事件没有引发一个对话框,其中显示消息“时间已到”...
我想问题可能是没有在用户界面线程上实现事件,所以我需要你的帮助来完成这个……
这是我的定时器代码
class SecondActivity : AppCompatActivity
{
int counter = 10;
private System.Timers.Timer _timer;
private int _countSeconds;
protected override void OnCreate(Bundle savedInstanceState)
{
_timer = new System.Timers.Timer();
//Trigger event every second
_timer.Interval = 1000;
_timer.Elapsed += OnTimedEvent;
//count down 5 seconds
_timer.Enabled = true;
_countSeconds = 5;
}
private void OnTimedEvent(object sender, System.Timers.ElapsedEventArgs e)
{
_countSeconds--;
if (_countSeconds == 0) {
_timer.Stop();
Switch switch1 = this.FindViewById<Switch>(Resource.Id.switch2);
Android.App.AlertDialog.Builder dialog = new Android.App.AlertDialog.Builder(this);
Android.App.AlertDialog alert = dialog.Create();
alert.SetTitle("");
alert.SetMessage("Simple Alert");
alert.SetButton("OK", (c, ev) =>
{
// Ok button click task
});
switch1.Checked = false;
}
我只想让 Elapsed 事件处理程序在变量倒计时为零时显示一个警告对话框,谢谢
在第一个评论将我指向一个相关问题后,我找到了实现用户线程的方法,它现在可以正常工作以显示警报对话框...
private void OnTimedEvent(object sender, System.Timers.ElapsedEventArgs e)
{
//This is how to make the Timer callback on the UI
RunOnUiThread(() =>
{
_countSeconds--;
if (_countSeconds == 0)
{
Switch switch1 = this.FindViewById<Switch>(Resource.Id.switch2);
Android.App.AlertDialog.Builder dialog = new Android.App.AlertDialog.Builder(this);
Android.App.AlertDialog alert = dialog.Create();
alert.SetTitle("Its over");
alert.SetMessage("Simple Alert");
alert.SetButton("OK", (c, ev) =>
{
// Ok button click task
});
alert.Show();
switch1.Checked = true;
}
});
}
我正在 Xamarin 上实现系统计时器 Android,但我遇到了一个问题,即当倒计时结束时,经过的事件没有引发一个对话框,其中显示消息“时间已到”... 我想问题可能是没有在用户界面线程上实现事件,所以我需要你的帮助来完成这个…… 这是我的定时器代码
class SecondActivity : AppCompatActivity
{
int counter = 10;
private System.Timers.Timer _timer;
private int _countSeconds;
protected override void OnCreate(Bundle savedInstanceState)
{
_timer = new System.Timers.Timer();
//Trigger event every second
_timer.Interval = 1000;
_timer.Elapsed += OnTimedEvent;
//count down 5 seconds
_timer.Enabled = true;
_countSeconds = 5;
}
private void OnTimedEvent(object sender, System.Timers.ElapsedEventArgs e)
{
_countSeconds--;
if (_countSeconds == 0) {
_timer.Stop();
Switch switch1 = this.FindViewById<Switch>(Resource.Id.switch2);
Android.App.AlertDialog.Builder dialog = new Android.App.AlertDialog.Builder(this);
Android.App.AlertDialog alert = dialog.Create();
alert.SetTitle("");
alert.SetMessage("Simple Alert");
alert.SetButton("OK", (c, ev) =>
{
// Ok button click task
});
switch1.Checked = false;
}
我只想让 Elapsed 事件处理程序在变量倒计时为零时显示一个警告对话框,谢谢
在第一个评论将我指向一个相关问题后,我找到了实现用户线程的方法,它现在可以正常工作以显示警报对话框...
private void OnTimedEvent(object sender, System.Timers.ElapsedEventArgs e)
{
//This is how to make the Timer callback on the UI
RunOnUiThread(() =>
{
_countSeconds--;
if (_countSeconds == 0)
{
Switch switch1 = this.FindViewById<Switch>(Resource.Id.switch2);
Android.App.AlertDialog.Builder dialog = new Android.App.AlertDialog.Builder(this);
Android.App.AlertDialog alert = dialog.Create();
alert.SetTitle("Its over");
alert.SetMessage("Simple Alert");
alert.SetButton("OK", (c, ev) =>
{
// Ok button click task
});
alert.Show();
switch1.Checked = true;
}
});
}