使用 Xamarin Forms,可以在 Xaml 或代码中的 DataPicker 中设置日期格式,如下例所示:
datepickerInstance.Format = "MMM yyyy";
或者
datepickerInstance.Format = "yyyy";
格式适合 TextEntry 中的值,但 Picker Wheel 本身仍显示完整日期,即 2015 年 1 月 14 日。使用“开箱即用的日期选择器”可以轻松更改还是我必须编写自定义渲染器?例如,如果我将格式设置为 'yyyy',我只会在 Picker Wheel 中看到年份列表。
最佳答案
Is this easy to change using the 'out of the box date picker' or do I have to write a Custom Renderer? So for example if I set the format to 'yyyy' I would just see a list of years in the Picker Wheel.
这将是相当困难的。您将需要一个自定义渲染器,但故事并没有就此结束。问题是没有 iOS 或 Android native 控件可以执行您的请求,因此您必须解析日期格式并制作自己的选择器。对于年份选择器,您可以使用一个简单的选择器来填充年份数字,对于完整日期,您可以使用日期选择器,但在某些情况下,您必须使用非常自定义且复杂的解决方案。
关于Xamarin.Forms DatePicker 日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27951901/