【问题标题】:Date binding doesn't bind on current date日期绑定不绑定当前日期
【发布时间】:2021-07-29 08:32:02
【问题描述】:

在 DatePicker 中使用 MinimumDate="{x:Static sys:DateTime.Now}" 时,在创建新项目而不选择日期(日期应自动选择当前日期)时不会这样做。

如果不更改集合视图中的日期,则默认为 0001 年 1 月 1 日。这是为什么呢?

当我选择一个日期时(即选择 2022 年 5 月 8 日),它工作正常,该日期将显示在集合视图中。只有当我不选择日期时,它才会默认为 1/1/0001。

NewItemViewModel.cs:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Input;
using Xamarin.Forms;
using xamarinMobileTest.Models;

namespace xamarinMobileTest.ViewModels
{
    public class NewItemViewModel : BaseViewModel
    {
        private string text;
        private string description;
        private DateTime dueDate;

        public NewItemViewModel()
        {
            SaveCommand = new Command(OnSave, ValidateSave);
            CancelCommand = new Command(OnCancel);

            this.PropertyChanged +=
                (_, __) => SaveCommand.ChangeCanExecute();
        }

        private bool ValidateSave()
        {
            return !String.IsNullOrWhiteSpace(text)
                && !String.IsNullOrWhiteSpace(description);
        }

        public string Text
        {
            get => text;
            set => SetProperty(ref text, value);
        }

        public DateTime DueDate
        {
            get => dueDate;
            set => SetProperty(ref dueDate, value);
        }
        public string Description
        {
            get => description;
            set => SetProperty(ref description, value);
        }

        public Command SaveCommand { get; }
        public Command CancelCommand { get; }

        private async void OnCancel()
        {
            // This will pop the current page off the navigation stack
            await Shell.Current.GoToAsync("..");
        }

        private async void OnSave()
        {
            Item newItem = new Item()
            {
                Id = Guid.NewGuid().ToString(),
                Text = Text,
                Description = Description,
                DueDate = DueDate
            };

            await DataStore.AddItemAsync(newItem);

            // This will pop the current page off the navigation stack
            await Shell.Current.GoToAsync("..");
        }
    }
}

【问题讨论】:

  • 你需要将值设置为,MinimumDate 是用于错误处理而不是值处理的属性
  • 我该怎么做?

标签: c# xamarin.forms datepicker


【解决方案1】:

像这样,就像我说的最小日期不设置值

<DatePicker MinimumDate="{Binding MinDate}"
        MaximumDate="{Binding MaxDate}"
        Date="{Binding SelectedDate}" />

您的 DueDate 未启动,这意味着它采用默认值,即 0001/01/01

    /* ... */
    private DateTime dueDate;

    public NewItemViewModel()
    {
        DueDate = DateTime.Today;
        SaveCommand = new Command(OnSave, ValidateSave);
        CancelCommand = new Command(OnCancel);

        this.PropertyChanged +=
            (_, __) => SaveCommand.ChangeCanExecute();
    }

    /* .... */

【讨论】:

  • 我只需要最短日期,因为它基本上是一个“到期日”。如何在视图模型中创建 minDate 以设置为当前时间?
  • 即使在我之前的方法中选择当前日期作为到期日,1/1/0001 结果仍然出现。唯一的解决方法是选择一个不同的日期,然后再次选择当前日期。只有这样,它才会在集合视图中显示为该日期
  • @Tanner 你能发布你当前模型的getter和setter吗?
  • 我的 ViewModel 或..? - 我没有约会模型
  • 非常感谢
猜你喜欢
  • 2010-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-11
  • 2018-04-10
  • 2016-02-14
相关资源
最近更新 更多