【问题标题】:Change datePicker month format in Xamarin在 Xamarin 中更改 datePicker 月份格式
【发布时间】:2019-11-26 10:56:36
【问题描述】:

我的应用程序中有一个 DatePicker,我想以这种格式显示日期:11/26/2019 所以我将Format 属性更改如下:

myDatePicker.Format = "MM/dd/yyyy"
myDatePicker.Date = DateTime.Now;

但是显示日期的格式是这样的: 2019 年 11 月 26 日

我可以为此做其他事情吗?

【问题讨论】:

  • 您的代码在我这边运行良好。您测试了哪个平台?您能否提供您设备的版本?
  • 我在 UWP 上。确切地说,我想要日期选择器中的格式,例如 11 26 2019。所以这是我不想更改的月份的格式,但它仍然得到月份名称。

标签: c# xamarin datepicker format


【解决方案1】:

UWP 的 Xamarin.forms 似乎存在问题。作为解决方法,您可以使用 Custom Renderer 重置 DatePicker 的格式。

在 UWP 项目中

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using xxx.UWP;
using Xamarin.Forms.Platform.UWP;
using Xamarin.Forms;
[assembly:ExportRenderer(typeof(DatePicker),typeof(MyPickerRenderer))]
namespace xxx.UWP
{
    public class MyPickerRenderer:DatePickerRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<DatePicker> e)
        {
            base.OnElementChanged(e);
            if(Control!=null)
            {
                Control.MonthFormat = "{month.integer}";
            }
        }
    }
}

我会立即在github上发布问题,如果有任何更新会回复你。

【讨论】:

  • Morehover,如果我想将日期选择器中的文本居中,也是一样的方法?
猜你喜欢
  • 2019-03-23
  • 2013-06-10
  • 1970-01-01
  • 2023-03-25
  • 2013-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多