【发布时间】:2021-11-13 13:41:54
【问题描述】:
将 DatePicker 中的日期绑定到以下内容的最佳方法是:
"startedAt": {
"year": 2021,
"month": 9,
"day": 11
}
在 json 中它看起来像这样,但这些将是 c# 属性中的三个单独的 int。我用于可观察集合和 Date 类的入口类:
public class ListEntry : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged([CallerMemberName] string name = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
public int? id { get; set; }
public Media media { get; set; }
public int? _score;
public int? score
{
get => _score;
set
{
if (value == _score)
return;
_score = value;
OnPropertyChanged();
}
}
public int _Progress;
public int progress
{
get => _Progress;
set
{
if (value == _Progress)
return;
_Progress = value;
OnPropertyChanged();
}
}
public string status { get; set; }
public Date startedAt { get; set; }
}
public class Date : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged([CallerMemberName] string name = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
public int? _year;
public int? year
{
get => _year;
set
{
if (value == _year)
return;
_year = value;
OnPropertyChanged();
}
}
public int? _month;
public int? month
{
get => _month;
set
{
if (value == _month)
return;
_month = value;
OnPropertyChanged();
}
}
public int? _day;
public int? day
{
get => _day;
set
{
if (value == _day)
return;
_day = value;
OnPropertyChanged();
}
}
}
我需要实现的是,当我在 DatePicker 中更改日期时,这三个属性也会发生变化。
我自己尝试过这样的事情
<DatePicker>
<DatePicker.Date>
<MultiBinding Mode="TwoWay" StringFormat="{}{0}/{1}/{2}">
<Binding Path="startedAt.month" TargetNullValue="2" />
<Binding Path="startedAt.day" TargetNullValue="3" />
<Binding Path="startedAt.year" TargetNullValue="2000" />
</MultiBinding>
</DatePicker.Date>
</DatePicker>
它可以正确选择日期,但更改日期不会更改属性,而且我遇到了错误。它似乎有问题,当我向下滚动页面并返回顶部时,它将返回原始绑定而不是我选择的日期。不行还是我做错了?
[0:] MultiBinding: '/20/2021' cannot be converted to type 'System.DateTime'
[0:] MultiBinding: '//' cannot be converted to type 'System.DateTime'
[0:] MultiBinding: '/20/1998' cannot be converted to type 'System.DateTime'
[0:] MultiBinding: '//' cannot be converted to type 'System.DateTime'
[0:] MultiBinding: '/16/2021' cannot be converted to type 'System.DateTime'
[0:] MultiBinding: '//' cannot be converted to type 'System.DateTime'
[0:] MultiBinding: '/1/2021' cannot be converted to type 'System.DateTime'
[0:] MultiBinding: '//' cannot be converted to type 'System.DateTime'
[0:] MultiBinding: '/20/2021' cannot be converted to type 'System.DateTime'
[0:] MultiBinding: '//' cannot be converted to type 'System.DateTime'
[0:] MultiBinding: '/23/2021' cannot be converted to type 'System.DateTime'
[0:] MultiBinding: '//' cannot be converted to type 'System.DateTime'
[0:] MultiBinding: '/20/2021' cannot be converted to type 'System.DateTime'
[0:] MultiBinding: '//' cannot be converted to type 'System.DateTime'
也许我需要某种转换器,但我不能真正改变类,我认为它可能不会反序列化?
编辑:
foreach (var Group in AnimeGroupObservable)
{
foreach (var Entry in Group)
{
if (Entry.startedAt.year == null)
{
Entry.startedAt.Time = new DateTime(1970, 01, 01);
}
else
{
Entry.startedAt.Time = new DateTime((int)Entry.startedAt.year, (int)Entry.startedAt.month, (int)Entry.startedAt.day);
}
}
}
我将 DateTime 添加到我的模型中
public DateTime? _Time;
public DateTime? Time
{
get => _Time;
set
{
if (value == _Time)
return;
_Time = value;
OnPropertyChanged();
}
}
【问题讨论】:
-
在模型中只包含一个 DateTime 属性会简单得多。 DateTime 有 Month/Day/Year 的属性,那么真的有必要声明自己的 Date 类型吗?
-
我可能会这样做。循环遍历项目以设置 DateTime 是否有效?因为我无法更改 API 模型,所以它不是我的。我正在使用我所拥有的。我用我已经完成的一些循环代码编辑了这篇文章。在这种情况下如何正确处理空值?日期可以来自 API。我在每个 Entry.StartedAt 之前添加了 (int),因为我的模型包含可为空的属性。
-
添加一个只读的 Datetime 属性以在您的模型类中组合这三个?
标签: xaml xamarin xamarin.forms mvvm