У меня очень простое приложение, в настоящее время содержащее только два класса - это " MainActivity.cs " и " NewDate.cs "
MainActivity просто подключает кнопку и элемент управления editText, а затем вызывает " NewDate.NewTimer () " - это просто запускает экземпляр таймера .NET.
Внутри «OnCreate» я могу успешно установить значение EditText, когда пользователь нажимает кнопку, однако, когда таймер истекает, я вызываю
SafeDate.MainActivity.SetTimerDoneText("Timer done!");
Используя точки останова, я могу определить, что приложение работает через "SetTimerDoneText", но строка
editTimerInfo.Text = Text;
не работает.
Любая помощь будет принята с благодарностью.
Оба класса ниже:
MainActivity.cs
public class MainActivity : Activity
{
static EditText editTimerInfo;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
Button btnNewTimer = FindViewById<Button>(Resource.Id.newDate);
editTimerInfo = FindViewById<EditText>(Resource.Id.editTimerInfo);
btnNewTimer.Click += (sender, e) =>
{
// Translate user's alphanumeric phone number to numeric
Core.NewDate.NewTimer();
// editTimerInfo.Text = "Timer started!"; //this works
};
}
public static void SetTimerDoneText(string Text)
{
//SetContentView(Resource.Layout.Main);//commented out - doesn't work
// EditText editTimerInfo = FindViewById<EditText>(Resource.Id.editTimerInfo); //commented out - doesn't work
editTimerInfo.Text = Text;
}
}
NewDate.cs
public static class NewDate
{
public static void NewTimer()
{
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 5000; //Miliseconds : 5000 = 1 second
aTimer.Enabled = true;
}
// Specify what you want to happen when the Elapsed event is raised.
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
SafeDate.MainActivity.SetTimerDoneText("Timer done!"); //Successfully enters the function in MainActivity.cs but won't set the EditText value
}
}