wpf第一次用,為了定時刷新這個頁面,也是費了我老大的功夫了,皇天不負有心人,最終還是給它弄出來了,話不多說,直接上代碼。
//定時器 private System.Timers.Timer timerNotice = null;
//定時刷新頁面
private void TimeRefresh_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
double countTime = 0; string timeStr = this.TimeRefreshRCB.SelectionBoxItem.ToString();
if ("關閉自動刷新".Equals(timeStr))
{
if (timerNotice != null) { timerNotice.Stop();
picTour.time.Visibility = Visibility.Hidden;
}
}
if ("開啟".Equals(timeStr))
{
if (timerNotice != null)
{
timerNotice.Stop();
}
countTime = 60 * 60 * 1000;
timerNotice = null;
picTour.time.Visibility = Visibility.Visible;
}
if(!"關閉自動刷新".Equals(timeStr) && timerNotice == null)
{
QueryData();
timerNotice = new System.Timers.Timer();
picTour.PicDate2.DateTimeText = staticVar.showTime.ToString();
timerNotice.Elapsed += new System.Timers.ElapsedEventHandler((o, eea) =>
{
QueryData();
});
timerNotice.Interval = countTime; timerNotice.Start();
}
}
其實代碼寫出來不難,想的過程比較復雜,QueryData()是執行查詢頁面數據的方法,需要強調的是這里QueryData()必須是靜態方法,TimeRefresh_SelectionChanged()是一個RadComboBox選擇下拉框,你可以選擇需要定時刷新的間隔。
