【发布时间】:2014-08-17 09:37:45
【问题描述】:
需要在固定时间间隔后自动更新 windows phone 页面中的内容,从 web 获取数据作为 json
数据是 json 格式的足球比赛实时比分
LiveScoresJson.cs 文件是
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Surfer
{
class LiveSocresJson
{
public class Customer
{
public string name { get; set; }
}
public class Match
{
public int id { get; set; }
public string status { get; set; }
public string home { get; set; }
public string homepath { get; set; }
public string away { get; set; }
public string awaypath { get; set; }
public string competition { get; set; }
public string competitionkey { get; set; }
public List<int> score { get; set; }
public List<object> events { get; set; }
public string scores { get; set; }
}
public class RootObject
{
public Customer customer { get; set; }
public List<Match> matches { get; set; }
}
}
}
我使用以下代码获取json数据
public async void JSON()
{
String Url = "url of the source";
WebClient Client = new WebClient();
Client.Headers["Accept"] = "application/json";
Client.DownloadStringAsync(new Uri(Url));
Client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(JsonFuction);
}
private void JsonFuction(object sender, DownloadStringCompletedEventArgs e)
{
try
{
var Data = JsonConvert.DeserializeObject<LiveSocresJson.RootObject>(e.Result);
foreach (var d in Data.matches)
{
string img = d.homepath;
string url = "Assets/Images/" + img + ".png";
`enter code here` d.homepath = url;
string img2 = d.awaypath;
string url2 = "Assets/Images/" + img2 + ".png";
d.awaypath = url2;
var f = d.score;
string scoresg = d.score[0] +"-" +d.score[1];
d.scores = scoresg;
}
TableContent.ItemsSource = Data.matches;
}
catch (Exception ex)
{ }
}
使用此代码,每次加载页面时都会加载内容,但是,我需要在固定时间间隔后更新页面,以便我可以在我的页面中保持实时比分更新,我该怎么办?
我使用的xaml代码是
<StackPanel >
<StackPanel Height="60" Background="Black" VerticalAlignment="Top" >
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" Height="60">
<TextBlock Text="Live Matches" Width="Auto" Foreground="White" FontSize="25" Margin="10,0,0,0" Height="40" HorizontalAlignment="Center" VerticalAlignment="Center" />
</StackPanel>
</StackPanel>
<Grid x:Name="La">
<ScrollViewer Name="ScrollViewer" Margin="0,0,0,12" VerticalAlignment="Top" VerticalScrollBarVisibility="Visible" ManipulationMode="Control">
<ListBox x:Name="TableContent" VerticalAlignment="Bottom" Height="505" Width="Auto">
<ListBox.ItemTemplate >
<DataTemplate>
<Grid x:Name="gh">
<StackPanel Height="100" Background="Transparent" Margin="0,0,0,0" Width="663" Orientation="Vertical" VerticalAlignment="Bottom">
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" Height="100" Width="Auto">
<TextBlock Text="{Binding status}" Width="Auto" Foreground="Black" FontSize="22" Margin="7,0,0,0" Height="40" VerticalAlignment="Center" />
<Image Source="{Binding homepath}" Height="40" Width="40" VerticalAlignment="Center" Margin="10,0,0,0"/>
<TextBlock Text="{Binding home}" Width="100" Foreground="Black" FontSize="22" Margin="10,0,0,0" Height="40" VerticalAlignment="Center" />
<TextBlock Text="{Binding scores}" Width="50" Foreground="Black" FontSize="25" Margin="10,0,0,0" Height="40" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<Image Source="{Binding awaypath}" Height="40" Width="40" VerticalAlignment="Center" Margin="15,0,0,0"/>
<TextBlock Text="{Binding away}" Width="Auto" Foreground="Black" FontSize="22" Margin="10,0,0,0" Height="40" VerticalAlignment="Center" />
</StackPanel>
</StackPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>
</Grid>
</StackPanel>
如何在此处使用 ObservableCollections 并通过将列表绑定到 ObservableCollections 而不是 Data.matches 来更改内容?
【问题讨论】:
-
为什么不每隔 X 分钟调用一次 JSON() 方法?
标签: c# json windows-phone-8