【问题标题】:How to auto update the content after fixed intervals in windows phone 8 page that retrieve data from Json如何在从 Json 检索数据的 Windows Phone 8 页面中的固定间隔后自动更新内容
【发布时间】: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


【解决方案1】:

您的列表框未绑定到集合。 这个参数是:

ItemsSource="{Binding Path=YourMatchResults}" 

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

一个很好的例子可以在这里找到:Binding observable collection to ListBox in XAML

从 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-Phone-81-samples-08631ca7

【讨论】:

    猜你喜欢
    • 2012-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多