【问题标题】:ListView Item does not appear to cast correctlyListView 项目似乎无法正确转换
【发布时间】:2018-01-14 04:44:04
【问题描述】:

我在 Xamarin.Forms XAML ListView 中有一个类型为 ItemSource 的来源:

ObservableCollection<WorkOrderActivity> serviceList = 
  new ObservableCollection<WorkOrderActivity>();
servicelistview.ItemsSource = serviceList;

这是我的桌子:

public class WorkOrderActivity
{

    string id;
    int workordernumber;
    string activity;
    string service;
    string activitylocation;
    float actualquantity;
    string reference;
    DateTime arrivaltime;
    DateTime departuretime;
    string remarks;
    bool complete;
    bool done;
    string uom;
    int startgauge;
    int endgauge;
    int contactgauge;

    [JsonProperty(PropertyName = "id")]
    public string Id
    {
        get { return id; }
        set { id = value; }
    }

    [JsonProperty(PropertyName = "workordernumber")]
    public int WorkOrderNumber
    {
        get { return workordernumber; }
        set { workordernumber = value; }
    }

    [JsonProperty(PropertyName = "activity")]
    public string Activity
    {
        get { return activity; }
        set { activity = value; }
    }

    [JsonProperty(PropertyName = "service")]
    public string Service
    {
        get { return service; }
        set { service = value; }
    }


    [JsonProperty(PropertyName = "arrivaltime" )]
    public DateTime ArrivalTime
    {
        get { return arrivaltime; }
        set { arrivaltime = value; }
    }

    [JsonProperty(PropertyName = "departuretime")]
    public DateTime DepartureTime
    {
        get { return departuretime; }
        set { departuretime = value; }
    }

    [JsonProperty(PropertyName = "activitylocation")]
    public string ActivityLocation
    {
        get { return activitylocation; }
        set { activitylocation = value; }
    }

    [JsonProperty(PropertyName = "actualquantity")]
    public float ActualQuantity
    {
        get { return actualquantity; }
        set { actualquantity = value; }
    }

    [JsonProperty(PropertyName = "startgauge")]
    public int StartGauge
    {
        get { return startgauge; }
        set { startgauge = value; }
    }

    [JsonProperty(PropertyName = "endgauge")]
    public int EndGauge
    {
        get { return endgauge; }
        set { endgauge = value; }
    }

    [JsonProperty(PropertyName = "contactgauge")]
    public int ContactGauge
    {
        get { return contactgauge; }
        set { contactgauge = value; }
    }

    [JsonProperty(PropertyName = "reference")]
    public string Reference
    {
        get { return reference; }
        set { reference = value; }
    }

    [JsonProperty(PropertyName = "remarks")]
    public string Remarks
    {
        get { return remarks; }
        set { remarks = value; }
    }

    [JsonProperty(PropertyName = "uom")]
    public string UOM
    {
        get { return uom; }
        set { uom = value; }
    }

    [JsonProperty(PropertyName = "done")]
    public bool Done
    {
        get { return done; }
        set { done = value; }
    }

    [JsonProperty(PropertyName = "complete")]
    public bool Complete
    {
        get { return complete; }
        set { complete = value; }
    }

    [Version]
    public string Version { get; set; }
    [CreatedAt]
    public DateTimeOffset CreatedAt { get; set; }
    [UpdatedAt]
    public DateTimeOffset UpdatedAt { get; set; }
    [Deleted]
    public bool Deleted { get; set; }
}

当我从列表视图中选择一个项目时出现问题,我得到了正确的记录,但日期时间字段中只有日期,时间为零。

private void servicelistview_ItemTapped(object sender, ItemTappedEventArgs e)
{
   var p = (WorkOrderActivity)e.Item;

当我在列表视图中显示记录时,它会显示正确的时间和日期,而当我将所选项目转换为 WorkOrderActivity 时,它会显示出来。

有什么想法吗?

【问题讨论】:

  • 演员阵容没有问题,你怎么查到没时间?
  • 我能看到 xaml 吗?

标签: c# xaml listview datetime xamarin.forms


【解决方案1】:

DateTime.Date 返回 00:00:00;来自 DateTime.Date 文档:

// Returns:
//     A new object with the same date as this instance, and the time value set to 12:00:00
//     midnight (00:00:00).

例子:

var dt = new DateTime(2018, 01, 14, 1, 2, 3);
Debug.WriteLine(dt.Date);
Debug.WriteLine(dt.Hour + " " + dt.Minute + " " + dt.Second);

输出:

1/14/2018 12:00:00 AM
1 2 3

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-14
    • 2015-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多