在Windows RT应用程序中每n秒刷新一次Web图片

时间:2014-03-19 17:49:47

标签: c# windows-8

我在VisualStudio中选择了一个样本并开始修改它以创建一个包含来自网络摄像头的图片的流量应用程序(每10秒更新一次)。我有一个SampleData.json文件,其中包含以下示例项目。

{"Groups":[
  {
    "UniqueId": "Group-1",
    "Title": "Primorska in Notranjska",
    "Subtitle": "Group subtitle: 1",
    "ImagePath": "Assets/Pics/primorska.jpg",
    "Description" : "Group Description: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus tempor scelerisque lorem in vehicula. Aliquam tincidunt, lacus ut sagittis tristique, turpis massa volutpat augue, eu rutrum ligula ante a ante",
    "Items":
    [
      {
        "UniqueId": "Group-1-Item-1",
        "Title": "A1/E61/E70 Ljubljana - Koper, priključek Brezovica",
        "Subtitle": "Item Subtitle: 1",
        "ImagePath": "Assets/LightGray.png",
        "Description" : "",
        "Content" : "A1/E61/E70 Ljubljana - Koper, priključek Brezovica, pogled proti Postojni",
        "PicSource" : "http://kamere.dars.si/kamere/ljubljana/K01_Brezovica_Po.jpg"
      },

比我将图片绑定到每个项目:

<Image Source="{Binding PicSource}" Grid.Row="2" Margin="0,88,440,10" 
       Stretch="UniformToFill" AutomationProperties.Name="{Binding Title}" Grid.ColumnSpan="2"/>

如何每隔n秒刷新一次?

编辑:我在SampleDataSource中设置了ViewModel:

        public SampleDataItem(String uniqueId, String title, String subtitle, String imagePath, String description, String content, String picSource)
    {
        this.UniqueId = uniqueId;
        this.Title = title;
        this.Subtitle = subtitle;
        this.Description = description;
        this.ImagePath = imagePath;
        this.Content = content;
        this.PicSource = picSource;
    }

..以后             public string PicSource {get;私人集; } ..后来             private async Task GetSampleDataAsync()         {             if(this._groups.Count!= 0)                 返回;

        Uri dataUri = new Uri("ms-appx:///DataModel/SampleData.json");

        StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(dataUri);
        string jsonText = await FileIO.ReadTextAsync(file);
        JsonObject jsonObject = JsonObject.Parse(jsonText);
        JsonArray jsonArray = jsonObject["Groups"].GetArray();

        foreach (JsonValue groupValue in jsonArray)
        {
            JsonObject groupObject = groupValue.GetObject();
            SampleDataGroup group = new SampleDataGroup(groupObject["UniqueId"].GetString(),
                                                        groupObject["Title"].GetString(),
                                                        groupObject["Subtitle"].GetString(),
                                                        groupObject["ImagePath"].GetString(),
                                                        groupObject["Description"].GetString());

            foreach (JsonValue itemValue in groupObject["Items"].GetArray())
            {
                JsonObject itemObject = itemValue.GetObject();
                group.Items.Add(new SampleDataItem(itemObject["UniqueId"].GetString(),
                                                   itemObject["Title"].GetString(),
                                                   itemObject["Subtitle"].GetString(),
                                                   itemObject["ImagePath"].GetString(),
                                                   itemObject["Description"].GetString(),
                                                   itemObject["Content"].GetString(),
                                                   itemObject["PicSource"].GetString()));
            }
            this.Groups.Add(group);
        }
    }

1 个答案:

答案 0 :(得分:0)

您可以从代码中设置来源:

public MainPage()
{
    this.InitializeComponent();

    DispatcherTimer timer = new DispatcherTimer();
    timer.Interval = TimeSpan.FromSeconds(n);
    timer.Tick += (sender, e) =>
    {
        //  ...
        //  image.Source = ...
    };
    timer.Start();           
}