這種寫法是直接寫在cs檔裡面,用DispatcherTimer來設定一個每秒count+1的計時器,並且加到5之後就停下來。
cs程式碼:
- private int count = 0;
- private DispatcherTimer timer;
- public MainWindow()
- {
- InitializeComponent();
- // set timer
- timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) };
- timer.Tick += TimerTick;
- timer.Start();
- }
- private void TimerTick(object sender, EventArgs e)
- {
- count++;
- if(count == 5)
- {
- timer.Stop();
- }
- }
End
沒有留言:
張貼留言