2017-05-09 60 views
0

这里是我想要做的。 我使用Xamarin Android,并试图做出24小时倒数计时器。C#/ Xamarin如何使24小时倒计时从一个点

  1. 定时器文本应每秒更新一次,当应用程序将重新启动,应该继续保存时间

DateTime timeStartedCountDown;

  • 计时器应从一个点计算时间。 (未运行时延迟)
  • 如)我想要什么=

    DateTime timeStartedCountDown;

    每秒

    DisplayLeftTime(24 + (timeStartedCountDown - currentTime))?一些像这样的

    不是我想要的=

    int timeElapsed; 
    

    每秒

    timeElapsed += 1; 
    text.Text = twentyFourHoursSec - timeElapsed 
    

    我不熟悉的时间&线程抱歉..

    PS。 android java代码很好!

    +0

    你有什么试过,你面临的问题是什么? – sachin

    回答

    0

    这会在您第一次打开应用程序时启动计时器。

    它通过使用ISharedPreferences回顾startTime。并在线程中每秒更新日期。

    public class MainActivity : Activity 
    { 
        DateTime startTime = DateTime.Now; 
        Thread thread; 
        protected override void OnCreate (Bundle savedInstanceState) 
        { 
         base.OnCreate (savedInstanceState); 
    
         ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences (this); 
         ISharedPreferencesEditor editor = prefs.Edit(); 
    
         long memStartTime = prefs.GetLong ("startTime", 0); 
         if (memStartTime != 0) { 
          startTime = new DateTime (memStartTime); 
         } else { 
          editor.PutLong ("startTime", startTime.Ticks); 
          editor.Apply(); 
         } 
         TimerUpdate(); 
        } 
    
        void TimerUpdate() { 
         if (thread == null) { 
          thread = new Thread (delegate() { 
           while (true) { 
            Thread.Sleep (1000); 
            DateTime curTime = DateTime.Now; 
            Console.WriteLine (curTime - startTime); 
            //Update your on screen textview. 
           } 
          }); 
          thread.Start(); 
         } 
        } 
    }