小编Puk*_*aai的帖子

C#WPF MVVM中的时间滴答

我正在做一个程序,每秒必须挂一个时钟,主要问题是我是WPF和MVVM的初学者:)

但是否则我的时钟运行起来并不令人耳目一新 我只有时间和日期目的的特殊课程.

这是我的代码:

TimeDate类:

public class TimeDate : ViewModelBase
{

    public string SystemTimeHours;
    string SystemTimeLong;
    string SystemDateLong;
    string SystemTimeZoneLong;

    string SystemTimeShort;
    string SystemDateShort;
    string SystemTimeZoneShort;



    public void InitializeTimer()
    {

        DispatcherTimer timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromSeconds(1);
        timer.Tick += new EventHandler(timer_Tick);
        timer.Start();
    }

    void timer_Tick(object sender, EventArgs e)
    {

        SystemTimeHours = DateTime.Now.ToString("HH:mm tt");

    }
}
Run Code Online (Sandbox Code Playgroud)

视图模型:

public class ViewModel : ViewModelBase
{
    public ViewModel()
    {
        TimeDate td = new TimeDate();
        td.InitializeTimer();
        HoursTextBox = td.SystemTimeHours;

    }



    private string _hourstextbox;
    public …
Run Code Online (Sandbox Code Playgroud)

c# wpf time mvvm ticker

5
推荐指数
2
解决办法
4019
查看次数

标签 统计

c# ×1

mvvm ×1

ticker ×1

time ×1

wpf ×1