Localization 将System.Globalization.DateTimeFormatInfo短日期格式转换为Windows.Globalization.DateTimeFormatting.DateTimeFormatter
我正在UWP中使用CalendarDatePicker控件。日期的格式应本地化为用户选择的区域性,这不一定与操作系统语言设置相同。我希望日期格式与myCultureInfo.DateTimeFormat.ShortDatePattern相同,例如mm/dd/yyyy。CalendarDatePicker公开DateFormat属性,该属性的格式应类似于{month.integer}/{day.integer}/{year.full}。我需要一个用于XAML绑定的转换器。似乎应该有一个简单的方法来做到这一点Localization 将System.Globalization.DateTimeFormatInfo短日期格式转换为Windows.Globalization.DateTimeFormatting.DateTimeFormatter,localization,datetime-format,uwp-xaml,globalization,cultureinfo,Localization,Datetime Format,Uwp Xaml,Globalization,Cultureinfo,我正在UWP中使用CalendarDatePicker控件。日期的格式应本地化为用户选择的区域性,这不一定与操作系统语言设置相同。我希望日期格式与myCultureInfo.DateTimeFormat.ShortDatePattern相同,例如mm/dd/yyyy。CalendarDatePicker公开DateFormat属性,该属性的格式应类似于{month.integer}/{day.integer}/{year.full}。我需要一个用于XAML绑定的转换器。似乎应该有一个简单
<!-- In XAML -->
<CalendarDatePicker
FirstDayOfWeek="{x:Bind ViewModel.CalendarFirstDayOfWeek, Mode=OneTime, Converter={StaticResource SystemDayOfWeekToWindowsDayOfWeekConverter}}"
DateFormat="{x:Bind ViewModel.DateFormat, Mode=OneWay, Converter={StaticResource DateTimeFormatToDateTimeFormatterTemplateConverter}"
Date="{x:Bind ViewModel.Date, Mode=TwoWay, Converter={StaticResource DateTimeToDateTimeOffsetConverter}}"/>
//Converter
public class DateTimeFormatToDateTimeFormatterTemplateConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
//What goes here?
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
编辑:
理想情况下,CalendarDatePicker只有一个CultureInfo属性来处理所有这些。我尝试了Language属性,但没有明显的效果。是的,您可以通过使用的属性设置,该属性告诉如何通过提供格式模板或格式模式的字符串来格式化值。该属性用于获取或设置应用于的本地化/全球化语言信息
不同的shortdate属性可能也有不同的日期格式。你可以看到en-US和fr-fr语言的不同
因此,您可能需要使用该类来获取当前的文化
对于转换,您可以从您的设备设置应用程序获取所有格式:设置=>时间和语言=>数据和时间=>更改日期和时间格式,在短日期部分,您可以看到所有格式。然后可以根据返回的字符串返回相应的字符串。以下是一些代码示例:
var shortDataPattern=CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern;
Debug.WriteLineshortDataPattern;
开关短数据模式
{
案例M/d/yyyy:
//返回短日期或以下日期
返回{}{month.integer}/{day.integer}/{year.full};
案例M/d/yy:
回来
…//其他格式
}
对于在这里冒险的任何人,这将至少本地化日期格式和一周的第一天,但不会本地化月份名称:
<CalendarDatePicker
Language="{x:Bind ViewModel.CalendarLanguageCode, Mode=OneWay}"
FirstDayOfWeek="{x:Bind ViewModel.CalendarFirstDayOfWeek, Mode=OneTime, Converter={StaticResource SystemDayOfWeekToWindowsDayOfWeekConverter}}"
DateFormat="shortdate"
Date="{x:Bind ViewModel.Date, Mode=TwoWay}"/>
SystemDayOfWeekToWindowsDayOfWeekConverter的实现是从System.DayOfWeek到Windows.Globalization.DayOfWeek的简单转换您需要自己进行转换。当您获得ShortDatePattern值时,您可以根据该值转换CalendarDatePicker的DateTimeFormatter,例如ShortDatePattern=M/d/yyyy,然后指定DateFormat=shortdate。@BreezeLiu MSFT您能用代码示例说明一下吗?CalendarDatePicker的DateTimeFormatter未公开,因此我无法在其上设置ShortDatePattern。看起来我只能访问DateFormat属性,然后将其传递给DateTimeFormatter构造函数。我也尝试过这样的方法:Language=en-UK-DateFormat=shortdate,但没有成功。更正:en-UK不是有效的语言代码。如果我使用有效的语言代码并显式设置日期格式(例如en GB)并绑定一周的第一天,则该功能可以正常工作。但是,所有这些仍然没有将日历弹出框中月份的名称本地化。谢谢。我确实希望避免为每种可能的日期格式编写switch语句。我发现如果同时设置语言和DateFormat属性,它将本地化日期格式。我没想到要显式地设置DateFormat。但是,现在的问题是,在设置Language属性时,月份名称没有本地化。有什么建议吗?