如何使用 xaml 在 Xamarin 中显示当前时间

Mal*_*eeb 1 c# asp.net xaml xamarin.forms

当我在 DatePicker 元素中使用DateTime.Today 时,它正在运行并为我提供正确的输出,但是当我在 TimePicker 元素中使用DateTime.Now 时,它会返回错误

  1. 错误 位置 17:21。无法分配属性“时间”:属性不存在,或不可分配,或值与属性之间的类型不匹配

主页.xaml

<DatePicker Date="{x:Static sys:DateTime.Today}" />
<TimePicker Time="{x:Static sys:DateTime.Now}" />
Run Code Online (Sandbox Code Playgroud)

Min*_*ipe 5

这是因为 上的TimePropertyTimePicker是 type TimeSpan,而 whileDateTime.Now是 type DateTime。您需要TimeOfDayDateTime对象上使用该属性。

编辑:事实证明,您不能DateTime.Now.TimeOfDay直接绑定到,因此您需要通过其他两种方式之一进行绑定:

1)BindingContext使用教程在页面的目标上定义一个属性

或 2) 给TimePicker一个名字并Time在后面的代码中设置它的属性