How to display message as invoked

Shay Wilner 1,726 Reputation points
2023-03-12T18:11:31.43+00:00

Hello

i have a list of sunset for each day for a period of a month like the date and time sunset

i would like to check every seconds if sunset time has arrived and then display a message.

i implemented using a timer but the message is not displayed

I forgot to mention it's in xamarin android When i launch the app let say 2 minutes before sunset string "before sunset" is displayed but when sunset time arrived the string doesn't change

private void OnTimedEvent(object sender, ElapsedEventArgs e)
        {

            DateTime da;
            String sunsetmessage;
            TimeSpan sunset, timeelapse;
            RunOnUiThread(() =>
            {
                Boolean flagchange = false;
                sunsetmessage = string.Empty;
                da = DateTime.UtcNow;
                   sunsetitem = Getsunsettime();
                    sunset = new TimeSpan(sunsetitem.hour, sunsetitem.minute, 0);
                    timeelapse = sunset - new TimeSpan(da.Hour , da.Minute, 59);
                if (timeelapse.TotalSeconds <= 0)
                    sunsetmessage = "after sunset";
                else if (timeelapse.TotalSeconds == 0)
                    sunsetmessage = " sunset";
                else
                    sunsetmessage = "before sunset";

                announcement.text = sunsetmessage;   
               
            });
        }

Thanks

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,312 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 70,556 Reputation points Microsoft Vendor
    2023-03-15T00:56:42.32+00:00

    What is the type of announcement control? Is it a TextView?

    I create a Xamarin android project with TextView and your code. The text of TextView will be changed at the runtime.

    By the way, before testing, please make sure your test device or emulator's time is correct. When I test your code, I find my emulator's time is differ from my local host's time.