从JSON幻灯片图像
问题描述:
我想创建一个图像幻灯片。这些照片是从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只,不能改变图片。我怎样才能克服这个问题?
答
而问题是只有幻灯片显示图像索引只有0,不能更改图片。
的foreach
环打破everything.If要进行幻灯片播放的图像,不包括DispatcherTimer
在foreach
循环,您可以通过以下步骤修改您的代码:
-
修改您
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(); } }
-
修改您的
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。
我建议你使用这个库来处理json http://www.newtonsoft.com/json和此实用程序从json创建c#类http://json2csharp.com/ – Quakenxt
我的问题是无法使幻灯片图像从json(只能拍摄json中的第一张图片,因此图片不会更改,仅闪烁)。我使用像上面的代码 – Rose