如何在固定的时间间隔后自动更新windows phone 8页面的内容,从Json检索数据

本文关键字:8页 phone 检索 Json windows 数据 时间 更新 | 更新日期: 2023-09-27 18:18:14

需要在固定的间隔后自动更新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来更改内容?

如何在固定的时间间隔后自动更新windows phone 8页面的内容,从Json检索数据

您的Listbox没有绑定到集合。它的参数是:

ItemsSource="{Binding Path=YourMatchResults}" 

现在你应该有一个名为"YourMatchResults"的ObservableCollection类型的属性,因为当ObservableCollection被改变时,ListBox总是刷新。(添加或删除的项目)但是如果您将整个集合替换为一个新的集合,绑定将丢失!所以你必须一个一个地更新它们,或者清除列表并添加项目。

在这里可以找到一个很好的例子:在XAML中将可观察集合绑定到ListBox

从WP8开始,应该使用LongListSelector而不是ListBox,因为它支持虚拟化并且只呈现显示的项。

LongListSelector的示例:

http://abundantcode.com/displaying-data-in-flat-list-in-windows-phone-8-longlistselector-control/

http://www.geekchamp.com/articles/the-new-longlistselector-control-in-windows-phone-8-sdk-in-depth

我建议下载这个包来获得一堆运行的例子,这些例子被减少到最大,仍然是可执行的,但演示了某个WP8功能:

http://code.msdn.microsoft.com/windowsapps/windows -电话- 81 -样品- 08631 ca7