点击来自 JSON 的幻灯片图片

Tapped Slideshow Image From JSON

我有一个图片幻灯片,当用户点击图片时,如果 json 中的 url 没有“#”,它将转到 url 地址。 JSON:

XAML:

<Image x:Name="topBanner" Source="images/new (3.0)/banner/MI-W10-banner-1366-01.png" Tapped="topBanner_Tapped" />

代码:

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();
                    Images1a.Add(image);
                }


                    playlistTimer1a = new DispatcherTimer();
                    playlistTimer1a.Interval = new TimeSpan(0, 0, 6);
                    playlistTimer1a.Tick += playlistTimer_Tick1a;
                    topBanner.Source = new BitmapImage(new Uri(Images1a[0]));
                    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();
        }

 private void topBanner_Tapped(object sender, TappedRoutedEventArgs e)
        {
            //I have to confused to add this code
           //Can anyone help me to add this code
        }

如何,当用户点击幻灯片图片时,它会转到JSON上的url地址(当地址url不是'#'时)?

代码有很多需要改进的地方。我建议您阅读更多有关 DataBinding 和 MVVM 的内容。

但是,我会尽力帮助您编写代码,原样:

首先,您需要确保维护 JSON 中的所有数据,以便日后使用。您需要一个结构来保存 urls:

而不是图像 URL 列表
public struct DataItem
{
   public string ImageUrl {get;set;}
   public string Url {get;set;}
}

然后将您的列表声明为:

List<DataItem> Images1a = new List<DataItem>();

构建列表时,创建 DataItem 实例并将它们添加到列表中

foreach (JsonValue groupValue1 in jsonData1)
{

    JsonObject groupObject1 = groupValue1.GetObject();

    var dataItem = new DataItem();
    dataItem.ImageUrl = groupObject1["image"].GetString();
    dataItem.Url = groupObject1["url"].GetString();

    Images1a.Add(dataItem);
 }

最后,当你点击一张图片时,根据索引找到url:

private async void topBanner_Tapped(object sender, TappedRoutedEventArgs e)
{
    var tappedItem = Images1a[count1a];
    if (tappedItem.Url != "#")
    {
        await Windows.System.Launcher.LaunchUriAsync(new Uri(tappedItem.Url));        
    }
}

您可以阅读有关如何从 documentation

启动 URI 的更多信息