如何在Windows Phone上发出json请求时显示加载消息?

时间:2013-07-31 16:48:11

标签: c# asynchronous windows-phone loading

我需要显示一条加载消息,而json请求是在windows phone上发出的,就像在Android上使用ProgressDialog的异步任务一样,我在onPreExecute()上放置了dialog.show()和dialog.dismiss( )on onPostExecute。我怎么能在Windows Phone上做到这一点?

这是我的json请求:

WebClient webClient = new WebClient();
    webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
    webClient.DownloadStringAsync(new Uri("https://maps.googleapis.com/maps/api/place/textsearch/json?&query=taxi&location=-19.94549444,-43.92314218&&radius=5000&sensor=true&key=AIzaSyDucC8QBV5wu4V-dQXFfABXGaaUzdmT5xw"));

在下载此请求时,我需要显示加载消息,并在请求完成时提交。

1 个答案:

答案 0 :(得分:3)

您可以显示进度条以指示正在进行的过程 只需将代码如下:

WebClient webClient = new WebClient();
webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);

ProgressIndicator progressIndicator = new ProgressIndicator() {
    IsVisible = true,
    IsIndeterminate = false,
    Text = "Loading..." 
};

SystemTray.SetProgressIndicator(this, progressIndicator);
webClient.DownloadStringAsync(new Uri("https://maps.googleapis.com/maps/api/place/textsearch/json?&query=taxi&location=-19.94549444,-43.92314218&&radius=5000&sensor=true&key=AIzaSyDucC8QBV5wu4V-dQXFfABXGaaUzdmT5xw"));

当您的请求开始执行时,这将显示进度条 要在加载后停止此操作,请将以下代码添加到事件处理程序:

void webClient_DownloadStringCompleted(s, e)
{
    Dispatcher.BeginInvoke( () =>
    {
        progressIndicator.IsVisible = false;
        // Your code
    });
}