在Windows Phone的XAML时间跨度字符串格式 [英] TimeSpan string format in Windows Phone XAML

查看:163
本文介绍了在Windows Phone的XAML时间跨度字符串格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图一个XAML的StringFormat适用于时间跨度属性。

I'm attempting to apply a XAML StringFormat to a TimeSpan property.

注意:此问题适用于的Windows Phone

有关WPF,正确的语法是:

For WPF, the correct syntax is:

Text="{Binding ElapsedTime,StringFormat={}{0:m\\:ss}}"

Silverlight的,它需要单引号:

For Silverlight, it needs single-quotes:

Text="{Binding ElapsedTime,StringFormat='{}{0:m\\:ss}'}"

但对于Windows Phone的,不管是什么我都试过了,输出总是完整的00:00:00.0000000。

But for Windows Phone, no matter what I have tried, the output is always the full "00:00:00.0000000".


  • 的StringFormat ='m\\:SS'

  • 的StringFormat ='{} {0:m\\:SS}

  • 的StringFormat ='m\:SS'

  • 的StringFormat = m\\:SS

  • StringFormat='m\\:ss'
  • StringFormat='{}{0:m\\:ss}'
  • StringFormat='m\:ss'
  • StringFormat=m\\:ss

有我莫名其妙地错过了正确的排列,或者这只是不可能因为某些原因在Windows Phone的?

Have I somehow missed the correct permutation, or is this just not possible for some reason in Windows Phone?

更新

我用了一个解决方法(回答以下),但仍希望古怪的解释,如果有人知道。 (我说怪诞,因为的StringFormat 的DateTime 物业工作得很好,只是不能与时间跨度的。)

I used a workaround (answer below), but would still like an explanation of the weirdness, if anyone knows. (I say "weirdness" because StringFormat works perfectly well with DateTime properties, just not with TimeSpan ones.)

更新#2

从2010年 一个非常类似的问题。或许这只是与旧和/或紧凑。NET版本的问题。或将这个答案的StringFormat = \ {0:h\\:mm\\ \\} )的工作?

A very similar question from 2010. Maybe this is just an issue with older and/or "compact" versions of .Net. Or would this answer (StringFormat=\{0:h\\:mm\}) work?

推荐答案

最后我用一种变通方法。

I ended up using a workaround.

<TextBlock Text="{Binding DurationStr}" />

通过在视图模型一个新的属性:

With a new property in the view-model:

public string DurationStr
{
    get { return Duration.ToString(@"m\:ss"); }
}

public TimeSpan Duration
{
    get { return _duration; }
    set
    {
        _duration = value;
        RaisePropertyChanged("Duration");
        RaisePropertyChanged("DurationStr");
    }
}
private TimeSpan _duration;

这篇关于在Windows Phone的XAML时间跨度字符串格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆