【问题标题】:Converting Seconds to Minutes:Seconds将秒转换为分钟:秒
【发布时间】:2011-08-22 00:45:14
【问题描述】:

我正在尝试通过转换总秒数来绑定 TextBlock 的 Text 属性,即

1004 到 Minutes:Seconds,我可以成功地从 XML 中提取 Seconds,但我不知道如何使用 Getter 和 Setter,因此我可以将 Seconds 转换为 Minutes:Seconds

我查看了TimeSpan,我知道它可以满足我的要求,但我不知道如何编写 getter 和 setter,以便它将整数值(秒)转换为 Minute:Seconds 格式。

到目前为止,这是我在课堂上所拥有的

public class Stats 
{
 public TimeSpan Time {get;set;}
}

任何帮助将不胜感激,

谢谢

约翰

【问题讨论】:

    标签: c# data-binding windows-phone-7


    【解决方案1】:

    要将它作为一个属性来做,你可以这样做:

    public class Stats {
       public TimeSpan Time { get; set; }
       public string TimeFormated { get { return Time.TotalMinutes + ":" + Time.Seconds; } }
    }
    

    虽然你真的应该在你的 XAML 中这样做,因为你所做的是布局:

    <StackPanel Orientation="Horizontal">
      <TextBlock Text={Binding Time.TotalMinutes}" />
      <TextBlock Text=":" />
      <TextBlock Text=={Binding Time.Seconds}" />
    </StackPanel>
    

    【讨论】:

    • 这不是在你有 2 分钟的时候给你 2:0 吗?
    【解决方案2】:

    会推荐这个转换器(因为当你真正想要 2:01 时,前两个答案会给你 2:1 -

    public class FriendlyTimeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            TimeSpan ts = TimeSpan.FromSeconds((int)value);
            return String.Format("{0}:{1:D2}", ts.Minutes, ts.Seconds);                
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    
    }
    

    Note the :D2 specifier for format strings.

    要使用它,您需要在绑定的同时指定它:

    <phone:PhoneApplicationPage.Resources>
        <util:FriendlyTimeConverter x:Key="FriendlyTimeConverter"/>
    </phone:PhoneApplicationPage.Resources>
    
    ...
    
    <TextBlock Text="{Binding timeRemaining, Converter={StaticResource FriendlyTimeConverter}}" Name="TimerDisplay" Grid.Column="4" HorizontalAlignment="Right" Margin="12,0" Style="{StaticResource PhoneTextTitle2Style}"></TextBlock>
    

    【讨论】:

      【解决方案3】:

      使用转换器。

      XAML:

      <phone:PhoneApplicationPage.Resources>
          <classes:TimeSpanConverter x:Key="c" />
      </phone:PhoneApplicationPage.Resources>
      ...
      <TextBlock Text="{Binding Time, Converter={StaticResource c}}" />
      

      C#:

      public class TimeSpanConverter : IValueConverter
      {
          public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              var time = (TimeSpan) value;
              return time.TotalMinutes + ":" + time.Seconds;
          }
      
          public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              throw new NotSupportedException();
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-10-29
        • 1970-01-01
        • 1970-01-01
        • 2012-06-15
        • 2015-06-02
        • 2011-09-01
        相关资源
        最近更新 更多