繁体   English   中英

DispatcherTimer使用MVVM更新TextBlock

[英]DispatcherTimer updating TextBlock with MVVM

道歉,如果这不值得问一个问题,但我很困惑为什么我正在做的事情不起作用。

我正在制作一个WP8应用程序,我想在屏幕上显示当前日期和时间(将刷新每分钟)。 我已经设法让日期工作正常,但时间不会出现在我身上,我已经按照了很多教程。 这可能是非常愚蠢的事情,但我无法弄清楚。 我也在使用对我来说很新的MVVM模式,所以它可能与它有关吗?

这是我的ViewModel类;

public class SleepTrackerViewModel : INotifyPropertyChanged
{
    private string _currentTime, _currentDate;
    public event PropertyChangedEventHandler PropertyChanged;

    public SleepTrackerViewModel()
    {
        CurrentDateText();
        DispatcherTimerSetup();
    }

    private void DispatcherTimerSetup()
    {
        DispatcherTimer dispatcherTimer = new DispatcherTimer();
        dispatcherTimer.Interval = TimeSpan.FromMinutes(1);
        dispatcherTimer.Tick += new EventHandler(CurrentTimeText);
        dispatcherTimer.Start();
    }

    private void CurrentDateText()
    {
        CurrentDate = DateTime.Now.ToString("dddd dd MMMM yyyy");
    }

    private void CurrentTimeText(object sender, EventArgs e)
    {
        CurrentTime = DateTime.Now.ToString("HH:mm");
    }

    public string CurrentTime
    {
        get { return _currentTime; }
        set
        {
            if (_currentTime != null)
                _currentTime = value;

            OnPropertyChanged("CurrentTime");
        }
    }

    public string CurrentDate
    {
        get { return _currentDate; }
        set
        {
            if (_currentDate != value)
                _currentDate = value;

            OnPropertyChanged("CurrentDate");
        }
    }

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

在我的XAML代码中,

            <TextBlock FontSize="25" Foreground="Red" HorizontalAlignment="Center" VerticalAlignment="Top" Text="{Binding CurrentTime}"/>

同样在XAML代码中,我将我的xmlns:local设置为我的ViewModel文件夹,并将UserControl.DataContext设置为正确的类。 正如我所提到的,日期显示正常。 时间确实在设计器视图中随机出现,但是当我运行代码时它就消失了。

谢谢。

我查看了你的代码,并想知道CurrentTime的setter。

下列:

  if (_currentTime != null)
       _currentTime = value;

应该是:

  if (_currentTime != value)
       _currentTime = value;

@WellerEE的答案是正确的,您也应该接受他的答案并对其进行投票。 :D他的答案解决了你实际问过的问题,这是一个很好的答案的特征。

但是,在查看代码时,我发现您的时钟会一次关闭多达一分钟,具体取决于您启动应用程序的时间。 它会更好,如果你的计时器所触发每秒

dispatcherTimer.Interval = TimeSpan.FromSeconds(1);

最后一个想法:你可能会确保你没有重新发明轮子 - 查看Charles Petzold的Awesome All Xaml ClockDigital Version

的确,您可能不需要所有图形标识,但是这里还是有一些不错的选择,例如如何将DateTime.Now嵌入到代码中,现在直接作为资源,以及如何直接绑定到DateTime对象。 这可能是这样的:

ClockTicker.cs:

// adapted from: ClockTicker.cs (c) 2006 by Charles Petzold
public class ClockTicker : DependencyObject
{
    public ClockTicker()
    {
        StartTimer();
    }

    void StartTimer()
    {
        DispatcherTimer timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(100) };
        timer.Tick += (s, e) => { DateTime = DateTime.Now; };
        timer.Start();
    }

    public DateTime DateTime
    {
        get { return (DateTime)GetValue(DateTimeProperty); }
        set { SetValue(DateTimeProperty, value); }
    }

    public static readonly DependencyProperty DateTimeProperty =
        DependencyProperty.Register("DateTime", typeof(DateTime), typeof(ClockTicker));
}

ClockView.xaml:

<UserControl.Resources>
    <local:ClockTicker x:Key="clock"/>
</UserControl.Resources>
<StackPanel>
    <TextBlock Text="{Binding Source={StaticResource clock}, StringFormat={}{0:dddd dd MMMM yyyy}, Path=DateTime}"/>
    <TextBlock Text="{Binding Source={StaticResource clock}, StringFormat={}{0:HH:mm}, Path=DateTime}"/>
</StackPanel>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM