来自JSON的幻灯片图片

时间:2016-08-02 09:17:07

标签: c# json image uwp slideshow

我想创建一个图片幻灯片。图片来自json上的数据(如下所示)。 JSON 我尝试使用以下代码:

DispatcherTimer playlistTimer1a = null;
List<string> Images1a = new List<string>();

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    ImageSource1a();
}

private async void ImageSource1a()
        {
            try
            {
                var httpClientHandler = new HttpClientHandler();
                httpClientHandler.Credentials = new System.Net.NetworkCredential("username", "password");
                var httpClient = new HttpClient(httpClientHandler);
                string urlPath = "http://";
                var values = new List<KeyValuePair<string, string>>
                {
                    new KeyValuePair<string, string>("platform","win"),     
                };
                HttpResponseMessage response = await httpClient.PostAsync(urlPath, new FormUrlEncodedContent(values));

                response.EnsureSuccessStatusCode();

                string jsonText = await response.Content.ReadAsStringAsync();
                JsonObject jsonObject = JsonObject.Parse(jsonText);
                //JsonObject jsonData1 = jsonObject["data"].GetObject();

                JsonArray jsonData1 = jsonObject["data"].GetArray();

                foreach (JsonValue groupValue1 in jsonData1)
                {

                    JsonObject groupObject1 = groupValue1.GetObject();

                    string image = groupObject1["image"].GetString();
                    string url = groupObject1["url"].GetString();

                    Banner file1 = new Banner();
                    file1.Image = image;
                    file1.URL = url;
                    Images1a.Add(file1.Image);
                    playlistTimer1a = new DispatcherTimer();
                    playlistTimer1a.Interval = new TimeSpan(0, 0, 6);
                    playlistTimer1a.Tick += playlistTimer_Tick1a;
                    topBanner.Source = new BitmapImage(new Uri(file1.Image));
                    playlistTimer1a.Start();
                }
            }
            catch (HttpRequestException ex)
            {
                RequestException();
            }
        }

 int count1a = 0;

void playlistTimer_Tick1a(object sender, object e)
        {
            if (Images1a != null)
            {
                if (count1a < Images1a.Count)
                    count1a++;

                if (count1a >= Images1a.Count)
                    count1a = 0;

                ImageRotation1a();
            }
        }

private async void ImageRotation1a()
        {
            OpacityTrans1.Begin();
        }

问题只是幻灯片显示图像索引为0,无法更改图片。我怎样才能克服这个问题?

1 个答案:

答案 0 :(得分:0)

  

问题只是幻灯片显示图像索引为0,无法更改图片。

foreach循环会破坏所有内容。如果您想制作幻灯片显示图片,请不要在DispatcherTimer循环中包含foreach,您可以按照以下步骤修改代码:

  1. 修改您的ImageSource1a方法,如下所示:

    private async void ImageSource1a()
    {
        try
        {
            ...
            foreach (JsonValue groupValue1 in jsonData1)
            {
                JsonObject groupObject1 = groupValue1.GetObject();
                string image = groupObject1["image"].GetString();
                string url = groupObject1["url"].GetString();
                Images1a.Add(image);
            }
               //I don't know what is Banner object used for but for this piece of codes, a banner object is not necessary.
                //Banner file1 = new Banner();
                //file1.Image = image;
                //file1.URL = url;
    
                playlistTimer1a = new DispatcherTimer();
                playlistTimer1a.Interval = new TimeSpan(0, 0, 6);
                playlistTimer1a.Tick += playlistTimer_Tick1a;
                topBanner.Source = new BitmapImage(new Uri(Images1a[0]));//set the current image to the first one
                playlistTimer1a.Start();
        }
        catch (HttpRequestException ex)
        {
            RequestException();
        }
    }
    
  2. 修改您的playlistTimer_Tick1a方法,如下所示:

    private void playlistTimer_Tick1a(object sender, object e)
    {
        if (Images1a != null)
        {
    
            if (count1a < Images1a.Count)
                count1a++;
    
            if (count1a >= Images1a.Count)
                count1a = 0;
            topBanner.Source = new BitmapImage(new Uri(Images1a[count1a]));
            ImageRotation1a();
        }
    }
    
  3. 更新:这是我的基本演示:SlideShowSample