【问题标题】:Current Date in Silverlight XAML TextBlockSilverlight XAML TextBlock 中的当前日期
【发布时间】:2011-01-26 16:53:14
【问题描述】:

我来自 Flex,在那里你可以在花括号内做任何事情。我试图让TextBlock 显示今天的日期和时间,而不仅仅是用 C# 编码。我尝试了以下许多不同的变体,但没有运气。

TextBlock Text="{Source=Date, Path=Now, StringFormat='dd/MM/yyyy'}"

我知道我可以设置一个属性 MyDate 并绑定到它,但为什么我不能直接绑定到 DateTime.Now 属性?

【问题讨论】:

    标签: silverlight xaml date binding textblock


    【解决方案1】:

    Silverlight 中的绑定需要 Source 对象或 Dependency 对象。您可以从该源对象绑定到属性(因此根据定义,您绑定到实例成员)或依赖属性。

    由于DateTime.Now 是一个静态属性,您无法在 Silverlight 中直接绑定到它,因此需要一些代码。下一个最好的事情是使用代码:-

    • 确保尽可能多地在 XAML 中表达您需要的内容
    • 以尽可能解耦的方式这样做。

    因此我们可以分析出我们需要两件事。

    1. 将 DateTime 的静态成员公开为某个对象的实例属性
    2. 有一些方法可以将 DateTime 格式化为所需的输出。

    为了处理第一项,我将创建一个StaticSurrogate 类,在其中我将为我们需要访问的静态属性创建实例属性:-

    public class StaticSurrogate
    {
        public DateTime Today { get { return DateTime.Today; } }
        public DateTime Now { get { return DateTime.Now; } }
    }
    

    现在我们需要一种格式化日期时间的方法。价值转换器是这项工作的正确工具,大量借鉴了Tim Heuer Blog:-

    public class FormatConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (parameter != null)
            {
                string formatterString = parameter.ToString();
    
                if (!String.IsNullOrEmpty(formatterString))
                {
                    return String.Format(culture, String.Format("{{0:{0}}}", formatterString), value);
                }
            }
    
            return (value ?? "").ToString();
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    有了这两个类,我们现在可以在 Xaml 中完成剩下的工作,首先我们需要资源中这些类的实例:-

    <UserControl.Resources>
        <local:StaticSurrogate x:Key="Static" />
        <local:FormatConverter x:Key="Formatter" />     
    </UserControl.Resources>
    

    现在我们可以连接TextBlock :-

    <TextBlock Text="{Binding Today, Source={StaticResource Static},
        Converter={StaticResource Formatter}, ConverterParameter='dd MMM yyy'}" />
    

    请注意,这种方法具有以下优点:-

    • 我们不需要向放置 TextBlock 的 UserControl 添加代码,也不需要摆弄任何数据上下文。
    • 静态资源可以放置在 App.Resources 中,这将使 TextBlock 的创建完全独立于必须向 UserControl 添加任何其他内容。
    • 可以单独修改用于显示日期的格式。
    • 可以轻松地将对其他静态属性的访问权限添加到 StaticSurrogate 类中。

    【讨论】:

    • 这对我来说是有道理的,但与 Flex 语法相比它确实显得极端:text="{new Date()}"。我想这一切都归结为 Silverlight 仅支持 XAML 中的一些标记扩展这一事实。不过,如果他们使花括号评估更加健壮,那就太好了。感谢您的帮助!
    【解决方案2】:

    即使您可以在 Silverlight 的 XAML 中声明 DateTime.Now(因为您可以在 WPF - http://soumya.wordpress.com/2010/02/12/wpf-simplified-part-11-xaml-tricks/ 中声明),您也会遇到时间不会更新的问题。如果您使用秒更新的本地计时器,您可以确保您的时间也会更新。

    public class LocalTimer : INotifyPropertyChanged
    {
        private DispatcherTimer timer;
    
        public LocalTimer()
        {
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1.0);
            timer.Tick += new EventHandler(TimerCallback);
            this.TimeFormat = "hh:mm:ss";
            this.DateFormat = "dddd, MMMM dd";
        }
    
        private void TimerCallback(object sender, EventArgs e)
        {
            PropertyChanged(this, new PropertyChangedEventArgs("FormattedDate"));
            PropertyChanged(this, new PropertyChangedEventArgs("FormattedTime"));
        }
    
        public bool Enabled
        {
            get { return this.timer.IsEnabled; }
            set { if (value) this.timer.Start(); else this.timer.Stop(); }
        }
    
        public string FormattedDate { get { return DateTime.Now.ToString(this.DateFormat); } set {} }
        public string FormattedTime { get { return DateTime.Now.ToString(this.TimeFormat); } set{} }
    
        public string TimeFormat { get; set; }
        public string DateFormat { get; set; }
    
        #region INotifyPropertyChanged Members
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        #endregion
    }
    

    在 xaml ala 中声明 this 的一个实例:

    <local:LocalTimer x:Key="theTime" Enabled="True" />
    

    并使用绑定来确保始终反映您的时间。

    <TextBlock Text="{Binding Source={StaticResource theTime}, Path=FormattedDate, Mode=OneWay}" x:Name="TodaysDate" />
    <TextBlock Text="{Binding Source={StaticResource theTime}, Path=FormattedTime, Mode=OneWay}" x:Name="CurrentTime" />
    

    【讨论】:

    • 这是最佳做法,还是有更优雅的更新方式?
    【解决方案3】:
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    
    Text="{Binding Source={x:Static sys:DateTime.Today}, StringFormat='Today is {0:dddd, MMMM dd}'}"
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 2012-11-20
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    • 2012-07-14
    • 1970-01-01
    相关资源
    最近更新 更多