我有一张图片幻灯片显示,当用户点击图片时,如果json中的网址没有“#”,则会转到网址。 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上的网址(当地址网址不是'#'时)?
答案 0 :(得分:0)
对代码进行了很多改进。我建议您阅读有关DataBinding和MVVM的更多信息。
但是,我会尝试按原样帮助您完成代码:
首先,您需要确保维护JSON中的所有数据,以便以后使用它。您需要具有保存两个网址的结构,而不是拥有图片网址列表:
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);
}
最后,点击图片时,根据索引找到网址:
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的详情