【发布时间】: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