Windows Phone NTP客户端(译文)
By S.F.
本文链接 https://www.kyfws.com/news/windows-phone-ntp-client/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 2 分钟阅读 - 537 个词 阅读量 0Windows Phone NTP客户端(译文)
原文地址:https://www.codeproject.com/Articles/237501/Windows-Phone-NTP-Client
原文作者:Joel Ivory Johnson
译文由本站翻译
前言
Windows Phone NTP客户端
下载源代码53 KB
我已经计划好一个应用程序,在该应用程序中我需要确定当前的时间.因此,我不想相信用户设备报告的时间.相反,我需要从服务器检索时间.我制作了一个NTP(网络时间协议)客户端来检索此信息.这是Mango之前在Windows Phone 7上无法完成的工作.但是,在秋季更新(Mango)中,可以访问套接字.
客户端的使用非常简单.至少,可以使用默认构造函数创建客户端,订阅" ReceivedTime"事件,然后调用" RequestTime"方法来发起请求.可以在UI线程以外的其他线程上调用ReceivedTime
事件,因此请记住在进行UI更新时使用调度程序.
这是一个使用代码显示系统时间和网络时间的客户端的示例.
public partial class MainPage : PhoneApplicationPage
{
private NtpClient _ntpClient;
public MainPage()
{
InitializeComponent();
_ntpClient = new NtpClient();
_ntpClient.TimeReceived += new EventHandler<NtpClient.TimeReceivedEventArgs>
(_ntpClient_TimeReceived);
}
void _ntpClient_TimeReceived(object sender, NtpClient.TimeReceivedEventArgs e)
{
this.Dispatcher.BeginInvoke(() =>
{
txtCurrentTime.Text =
e.CurrentTime.ToLongTimeString();
txtSystemTime.Text =
DateTime.Now.ToUniversalTime().
ToLongTimeString();
});
}
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
}
private void UpdateTimeButton_Click(object sender, RoutedEventArgs e)
{
_ntpClient.RequestTime();
}
}
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
Windows-Phone-7 新闻 翻译