xaml – 如何在Windows Store Universal App(W8.1 WP8.1)中格式化日期?

我玩新 Windows Store通用应用程序模板,可用于Windows 8.1和Windows Phone 8.1,并想知道如何格式化字符串在XAML代码.

我试过了(XAML):

<TextBlock Text="{Binding TestItem.CurrentDate,StringFormat={}{0:MM/dd/yyyy}}" />

问题是StringFormat在Windows.UI.Xaml.Controls.TextBox中不可用.

Microsoft已经创建了一个sample project,这是关于格式化日期.但是使用的方法是基于(丑))代码.

所以,这是我的问题:

>为什么StringFormat在Windows Store Universal Apps中不可用?
>如何使用XAML代码格式化字符串?

编辑:
我决定使用转换器解决方案,对于那些感兴趣的是代码

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,string language)
    {
        throw new NotImplementedException();
    }
}

我很高兴为每一个建议如何改进上面的代码,随时评论.

谢谢MikaelDúiBolinder和Martin Suchan的建议/回答.

Windows运行时项目类型中的DataBinding不支持StringFormat属性,您具有的选项如下:

>在viewmodel中使用已格式化的日期作为get-only属性.
>使用转换器 – 您甚至可以创建StringFormatConverter,您可以在其中传递DateTime格式为ConverterParameter. Here’s a solution如何这样StringFormatConverter可以工作.

相关文章

Windows2012R2备用域控搭建 前置操作 域控主域控的主dns:自...
主域控角色迁移和夺取(转载) 转载自:http://yupeizhi.blo...
Windows2012R2 NTP时间同步 Windows2012R2里没有了internet时...
Windows注册表操作基础代码 Windows下对注册表进行操作使用的...
黑客常用WinAPI函数整理之前的博客写了很多关于Windows编程的...
一个简单的Windows Socket可复用框架说起网络编程,无非是建...