简体   繁体   English

使用 SQLite 在 Xamarin.Forms 中显示日期时间

[英]Displaying DateTime in Xamarin.Forms with SQLite

I am creating a Xamarin.Forms application for a school project.我正在为学校项目创建 Xamarin.Forms 应用程序。 This project displays a list of school terms (6-month blocks of time) with the start and end dates.此项目显示包含开始和结束日期的学期列表(6 个月的时间段)。 The list of the terms displays without issues, but the dates do not appear as expected: all dates are 01/01/0001.术语列表显示没有问题,但日期未按预期显示:所有日期都是 01/01/0001。 The terms table is:术语表是:

    Id          TermName    StartDate   EndDate   
----------  ----------  ----------  ----------
1           Term 1      2020-01-01  2020-06-30
2           Term 2      2020-07-01  2020-12-31
3           Term 3      2021-01-01  2021-06-30
4           Term 4      2021-07-01  2021-12-31
5           Term 5      2022-01-01  2022-06-30
6           Term 6      2022-07-01  2022-12-31

Since SQLite doesn't have a specific DATETIME data type, I'm storing the StartDate and EndDate as a TEXT data type, but the terms.cs model file uses the DateTime datatype.由于 SQLite 没有特定的 DATETIME 数据类型,因此我将 StartDate 和 EndDate 存储为 TEXT 数据类型,但 terms.cs 模型文件使用 DateTime 数据类型。 Placing a breakpoint at the loading of the list of terms shows: Breakpoint data .在术语列表的加载处放置一个断点显示: Breakpoint data

Terms.cs file:条款.cs文件:

[Table("Terms")]
    public class Terms
    {
        public int Term { get; set; }
        public string TermName { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
    }

MainPage.xaml.cs file: MainPage.xaml.cs 文件:

protected override void OnAppearing()
        {
            base.OnAppearing();
            using (SQLiteConnection conn = new SQLiteConnection(App.DBLocation))
            {
                conn.CreateTable<Terms>();
                var terms = conn.Table<Terms>().ToList();
                TermsListView.ItemsSource = terms;
                TermsListView.SelectedItem = null;
            }
        }

How do I get the StartDate and EndDate from my table to show up in my ListView correctly?如何从我的表中获取 StartDate 和 EndDate 以正确显示在我的 ListView 中?

Try this code in ListView在 ListView 中试试这个代码

     <Label
                                        FontSize="18"
                                        HorizontalOptions="Center"
                                        HorizontalTextAlignment="Center"
                                        Text="{Binding StartDate, StringFormat='{0:dd/MM/yyyy}'}" />
<Label
                                        FontSize="18"
                                        HorizontalOptions="Center"
                                        HorizontalTextAlignment="Center"
                                        Text="{Binding EndDate, StringFormat='{0:dd/MM/yyyy}'}" />

Because you store it as a string the value cannot be assigned to the DateTime properties (no automatic conversion).由于您将其存储为string ,因此无法将值分配给DateTime属性(无自动转换)。 So they keep their default date.所以他们保留他们的默认日期。 Change the properties to:将属性更改为:

 public class Terms
    {
        public int Term { get; set; }
        public string TermName { get; set; }
        public string StartDate { get; set; }
        public string EndDate { get; set; }
    }

Then display these properties or convert the value to a real DateTime type:然后显示这些属性或将值转换为真正的DateTime类型:

public class Terms
        {
            public int Term { get; set; }
            public string TermName { get; set; }
            public string StartDate { get; set; }
            public string EndDate { get; set; }
            public DateTime RealStartDate => DateTime.Parse(StartDate);
            public DateTime RealEndDate => DateTime.Parse(EndDate);
        }

The Parse method is not tested by me but you can look up the correct syntax if the result is not what you need. Parse方法未经我测试,但如果结果不是您需要的,您可以查找正确的语法。

In you View you should then use the RealStartDate and RealEndDate properties在您的视图中,您应该使用RealStartDateRealEndDate属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM