Joe*_*oel 10 xaml windows-store-apps windows-8.1 windows-phone-8.1 win-universal-app
我正在使用可用于Windows 8.1和Windows Phone 8.1的新Windows Store Universal App模板,并且想知道如何在XAML代码中格式化字符串.
我尝试了什么(XAML):
<TextBlock Text="{Binding TestItem.CurrentDate, StringFormat={}{0:MM/dd/yyyy}}" />
Run Code Online (Sandbox Code Playgroud)
问题是StringFormat没有Windows.UI.Xaml.Controls.TextBox.
Microsoft已经创建了一个示例项目,该项目与格式化日期有关.但是那里使用的方法基于(丑陋的)代码.
所以,这是我的问题:
StringFormatWindows Store Universal Apps不提供?public class DateConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if (value == null)
return null;
if (!(value is DateTime))
return null;
var dateTime = (DateTime)value;
var dateTimeFormatter = new DateTimeFormatter(YearFormat.Full,
MonthFormat.Full,
DayFormat.Default,
DayOfWeekFormat.None,
HourFormat.None,
MinuteFormat.None,
SecondFormat.None,
new[] { "de-DE" },
"DE",
CalendarIdentifiers.Gregorian,
ClockIdentifiers.TwentyFourHour);
return dateTimeFormatter.Format(dateTime);
}
public object ConvertBack(object value, Type targetType, object parameter,
string language)
{
throw new NotImplementedException();
}
}
Run Code Online (Sandbox Code Playgroud)
我很高兴每一条建议如何改进上面的代码,随时评论.
感谢MikaelDúiBolinder和Martin Suchan的建议/答案.
| 归档时间: |
|
| 查看次数: |
6479 次 |
| 最近记录: |