【问题标题】:Binding three properties to DatePicker将三个属性绑定到 DatePicker
【发布时间】: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


【解决方案1】:

您可以尝试使用 Entry 中的 Completed 事件和 DatePicker 中的 DateSelected 事件来设置它们之间的链接。更改条目中的文本后更改日期,并在选择日期后更改文本。 这是我的示例页面和代码:

xmal:

<StackLayout x:Name="stk"s/>
<Entry x:Name="E_Year" Completed="Entry Completed"/>
<Entry x:Name="E_ Month" Completed="Entry Completed"/>
<Entry x:Name="E_Day" Completed="Entry Completed"/>
<DatePicker x:Name"mydtp" DateSelected="0nDateSelected"/>
</StackLayout>

代码隐藏:

private void Entry _Completed(object sender, EventArgs e)
{

int _year = Convert. ToInt16(E_Year.Text);

int _month = Convert. ToInt16(E_Month.Text);

int _day = Convert.ToInt16(E_Day.Text);

DateTime _dateTime = new DateTime(_year,_month,_day);
mydtp.Date =_dateTime;
}

private void OnDateSelected (object sender, EventArgs e)
{
DateTime dateTime = mydtp.Date;

E_Year.Text = dateTime.Year.ToString();
E_Month.Text = dateTime Month.ToString();
E_Day.Text = dateTime.Day.ToString();

}

【讨论】:

    猜你喜欢
    • 2019-12-15
    • 2015-11-04
    • 2010-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多