在Silverlight中每两分钟更新一次当前时间

时间:2011-04-08 09:33:04

标签: silverlight wcf

我有Silverlight应用程序,它具有当前时间和日期,现在我从WCF服务获取此信息,我想在每分钟更新当前日期和时间。

如何实现?

我的服务代码是:

[OperationContract]
        public DateTime GetDateTime()
        {
            return DateTime.Now ;  
        }

我的xaml.cs是:

void Welcome_Loaded(object sender, RoutedEventArgs e)
        {
            SessionService.SLWebServiceClient client = new SessionService.SLWebServiceClient();
            client.GetDateTimeCompleted += new EventHandler<SessionService.GetDateTimeCompletedEventArgs>(client_GetDateTimeCompleted);
            client.GetDateTimeAsync(); 


        }

        void client_GetDateTimeCompleted(object sender, SessionService.GetDateTimeCompletedEventArgs e)
        {
            lblDateTime.Text = e.Result.ToString();  
        }

感谢。

1 个答案:

答案 0 :(得分:1)

您可以使用DispatcherTimer每两分钟调用一次WCF服务。以下是:

// Every 2 minutes calls the timer_Tick handler
DispatcherTimer timer = new DispatcherTimer() { Interval = new TimeSpan(0, 2, 0) };
timer.Tick += new EventHandler(timer_Tick);
timer.Start();

private void timer_Tick(object sender, EventArgs e)
{
   SessionService.SLWebServiceClient client = new SessionService.SLWebServiceClient();
   client.GetDateTimeCompleted += new EventHandler<SessionService.GetDateTimeCompletedEventArgs>(client_GetDateTimeCompleted);
   client.GetDateTimeAsync();
}

希望它有所帮助!

布鲁诺