微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

显示自最近数据库 DateTime 以来经过的时间,并且每经过一秒增加一次

如何解决显示自最近数据库 DateTime 以来经过的时间,并且每经过一秒增加一次

我有一个数据库,用于存储新书进入系统时的日期时间。在 Xamarin.Forms 中,我希望以 HH:MM:SS 显示数据库中最近的 DateTime 以来的时差。

当前,标签显示时差 (HH:MM:SS:MS) 但不会增加。没有新书进入时,显示的时间差应每增加一秒。

我想使用循环来每秒调用 CalculateTimeDifference() 函数,但我想知道是否有更有效的解决方案。

MainPage.xaml:

<StackLayout Orientation="Horizontal" HorizontalOptions="Center">
       <Label x:Name="labelTimeSince" Text="00:00:00" VerticalOptions="Center"/>
       <Button Text="Add New" x:Name="BtnAdd" Clicked="BtnAdd_Clicked"></Button>
</StackLayout>

MainPage.cs:

protected override void OnAppearing()
{
     base.OnAppearing();
     CalculateTimeDifference();
}

void BtnAdd_Clicked(object sender,EventArgs e)
{
      Book book = new Book 
      {
          BookSaveTime = DateTime.Now
      };
      App.Database.SaveBook(book);

      CalculateTimeDifference();
}

void CalculateTimeDifference()
{
       var latestBook = App.Database.GetRecentBookDate().FirstOrDefault();
       var timeDifference = DateTime.Now - latestBook.BookSaveTime;
       this.labelTimeSince.Text = timeDifference.ToString();
}

数据库.cs:

public int SaveBook(Book book)
{
      return database.Insert(book);
}

public List<Book> GetRecentBookDate()
{
      return database.Query<Book>("SELECT * FROM Book ORDER BY BookSaveTime DESC LIMIT 1;");
}

Book.cs:

public class Book: INotifyPropertyChanged
{
    [PrimaryKey,AutoIncrement]
    public int ID { get; set; }
    private DateTime bookSaveTime;
    public DateTime BookSaveTime
    {
        get
        {
            return bookSaveTime;
        }
        set
        {
            if (bookSaveTime != value)
            {
                bookSaveTime= value;
                OnPropertyChanged("BookSaveTime");
            }
        }
    }
}

我该怎么做:

  1. 格式化显示时差的方式(HH:MM:SS - 没有毫秒)。
  2. 在没有存储新书的情况下,每秒持续增加一秒的时间差

解决方法

您可以使用 Device.StartTimer

MainPage.cs:

private Book latestBook;

protected override void OnAppearing()
{
     base.OnAppearing();
     latestBook = App.Database.GetRecentBookDate().FirstOrDefault(); 
     Device.StartTimer(TimeSpan.FromSeconds(1),() => 
     {
         // BeginInvokeOnMainThread is needed because CalculateTimeDifference deals 
         // with user interface,which is allowed from main thread only
         Device.BeginInvokeOnMainThread(() => CalculateTimeDifference()); 
         return true; // let the timer continue work
     });
}

void BtnAdd_Clicked(object sender,EventArgs e)
{
      Book book = new Book 
      {
          BookSaveTime = DateTime.Now
      };
      App.Database.SaveBook(book);
      latestBook = book; // because the app is single-user,we don't need to ask 
      // the database every time,just to store the book we added will be enough
}

void CalculateTimeDifference()
{
       if (latestBook == null) // no books were stored before
       {
          this.labelTimeSince.Text = "-";
       }
       else
       {
          var timeDifference = DateTime.Now - latestBook.BookSaveTime;
          this.labelTimeSince.Text = timeDifference.ToString("HH:mm:ss");
       }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。