来自 JSON 的幻灯片图片

Slideshow image from 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而已,无法更改图片。我该如何克服这个问题?

And the problem is only a slideshow displays the image index to 0 only, can not change the picture.

foreach循环中断everything.If你想制作幻灯片图片,不要在foreach循环中包含DispatcherTimer,你可以通过以下方式修改你的代码步骤:

  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();
        }
    }
    

更新: 这是我的基本演示:SlideShowSample.