如何在Xamarin.Forms中将DatePicker的DateTime转换为Realm的DateTimeOffset?

我有一个类,该类是从RealmObject继承的,带有Birthday属性,该类是使用DatePicker输入的。但是Realm仅适用于DateTimeOffset。我怎样才能优雅地转换所有这些?

XAML

<DatePicker MinimumDate="01/01/1400"
                        MaximumDate="11/14/2019"
                        Date="{Binding Author.Birthday}" />

班主任

    public class Author : RealmObject
{
    //[PrimaryKey]
    public string Id { get; set; } = Guid.NewGuid().ToString();
    public string Name { get; set; }
    public string PhotoPath { get; set; }
    [Ignored]
    public Image Photo { get; set; }
    public DateTimeOffset? Birthday { get; set; }
    public IList<Book> Books { get; }
    public bool IsFavorite { get; set; }
}
shiyan5168 回答:如何在Xamarin.Forms中将DatePicker的DateTime转换为Realm的DateTimeOffset?

  1. 借助价值转换器 this
  2. 借助this
  3. 将DateTime转换为DateTimeOffset
  4. 如果同时使用该选择器显示数据,则需要实现值转换器的ConvertBack方法。将偏移量转换为DateTime就像返回一样容易:offset.DateTime

深入了解DateTimeOffset:link

本文链接:https://www.f2er.com/3086251.html

大家都在问