0
当我使用这个时,我必须重新启动应用程序才能刷新数据。有几个帖子说我应该在最后加上一个随机数字,但是我不能这么做,因为我为了工作而放了一个数字。刷新Webclient获取新数据
解决方案是什么?
void myButton_Click(object sender, RoutedEventArgs e)
{
i = 0;
lstService.Items.Clear();
lstDestination.Items.Clear();
listTime.Items.Clear();
if (textSearchBox.Text == "Cranleigh Avenue")
{
textBlock2.Text = textSearchBox.Text;
txtstopId.Text = "6445";
}
if (textSearchBox.Text == "Cranleigh Avenue (briamaw)")
{
textBlock2.Text = textSearchBox.Text;
txtstopId.Text = "7099";
}
else if (textSearchBox.Text == "Brighton Station (stop A)")
{
textBlock2.Text = textSearchBox.Text;
txtstopId.Text = "7052";
}
else if (textSearchBox.Text == "Brighton Station (stop B)")
{
textBlock2.Text = textSearchBox.Text;
txtstopId.Text = "7053";
}
else if (textSearchBox.Text == "Brighton Station (stop C)")
{
textBlock2.Text = textSearchBox.Text;
txtstopId.Text = "7054";
}
else if (textSearchBox.Text == "Brighton Station (stop D)")
{
textBlock2.Text = textSearchBox.Text;
txtstopId.Text = "7055";
}
else if (textSearchBox.Text == "Sea Life Centre (stop K)")
{
textBlock2.Text = textSearchBox.Text;
txtstopId.Text = "7642";
}
else if (textSearchBox.Text == "Race Hill")
{
textBlock2.Text = textSearchBox.Text;
txtstopId.Text = "6724";
}
string myvar = txtstopId.Text;
textSearchBox.Text = "";
try
{
WebClient webClient = new WebClient();
Uri uri = new Uri("http://www.URL.aspx?stopid=" + HttpUtility.UrlEncode(myvar));
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
webClient.OpenReadAsync(uri);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public int i;
void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
DataContractJsonSerializer ser = null;
try
{
ser = new DataContractJsonSerializer(typeof(RootContainer));
RootContainer rootContainer = ser.ReadObject(e.Result) as RootContainer;
foreach (Departures em in rootContainer.Departures)
{
i = i + 1;
if (i < 9)
{
string id = em.ServiceName;
string dt = em.Destination;
string tm = em.DepartureTimeAsString;
lstService.Items.Add(id);
lstDestination.Items.Add(dt);
listTime.Items.Add(tm);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
编辑
WebClient webClient = new WebClient();
string url = string.Format("http://www.URL.aspx?stopid={0}&ts={1}", HttpUtility.UrlEncode(myvar) + DateTime.Now.Ticks);
Uri uri = new Uri(url);
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
webClient.OpenReadAsync(uri);
,当我在上面替换此,它抛出一个formatexpection 'WebClient的Web客户端=新的Web客户端(); string url = string.Format(“http://www.URL.aspx?stopid = {0}&ts = {1}”,HttpUtility.UrlEncode(myvar)+ DateTime.Now.Ticks); Uri uri = new Uri(url); webClient.OpenReadCompleted + = new OpenReadCompletedEventHandler(webClient_OpenReadCompleted); webClient.OpenReadAsync(uri); ' 编辑 - 只是加了它 – 2012-04-03 15:02:43
你可以把它放在主要问题。它格式化的事情更容易。 – gbanfill 2012-04-03 15:04:28
它应该是myVar和DAteTime.Now.Ticks之间的逗号,格式为:string.Format(“url?stopid = {0}&ts = {1}”,HttpUtility.UrlEncode(myvar),DateTime.Now.Ticks) ; – gbanfill 2012-04-03 15:07:47