[UWP] [C#]使用JSON中的图像更改评级 [英] [UWP][C#]Change Rating using image from JSON

查看:81
本文介绍了[UWP] [C#]使用JSON中的图像更改评级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示"评级" gridview中的数据使用下图:

例如,评级值为5,它将显示为如下:

如果等级为3,它将是显示如下:

如果评级为0,则为将显示如下:

代码:

try
                            {
                                loading.Visibility = Visibility.Visible;
                                string urlPath1 = "http://.../results.json?module=listing&page=1&token=3f63-dc43-c8d5-eb45-8cbf-b72d-9d98-800f";
                                var httpClient1 = new HttpClient(new HttpClientHandler());

                                var values1 = new List<KeyValuePair<string, string>>
                                {

                                };
                                HttpResponseMessage response1 = await httpClient1.GetAsync(urlPath1);
                                response1.EnsureSuccessStatusCode();

                                if (!response1.IsSuccessStatusCode)
                                {
                                    busyIndicator.IsActive = false;
                                    RequestException();
                                }
                                string jsonText1 = await response1.Content.ReadAsStringAsync();
                                JsonObject jsonObject1 = JsonObject.Parse(jsonText1);
                                JsonArray jsonData1 = jsonObject1["data"].GetArray();

                                foreach (JsonValue groupValue1 in jsonData1)
                                {
                                    JsonObject groupObject2 = groupValue1.GetObject();
string title = groupObject2["title"].GetString();
    double rating = groupObject2["rating"].GetNumber();


                                    ListingClass file1 = new ListingClass();
                                    file.Title = title;
                                    file1.Rating = Convert.ToInt32(rating);

                                    listingDatasource.Add(file1);
                                }
                                itemGridView.ItemsSource = listingDatasource;
                                busyIndicator.IsActive = false;
                            }

                            catch (HttpRequestException ex)
                            {
                                busyIndicator.IsActive = false;
                                RequestException();
                            }



如何使用图片显示评分?

How do you show the rating using the picture?


注意:评级的最大值是5

Note: The maximum value of the rating is 5




推荐答案

你好,

你可以提取image_uri字符串然后将表单JSON传递给BitmapImage构造函数,然后将BitmapImage设置为ImageControl的源。

You can extract the image_uri string form JSON then pass it to BitmapImage constructor and after set that BitmapImage as a source of your ImageControl.

MyImageControl.Source = new BitmapImage(new Uri(image_uri));

但是有更好的方法可以使用JSON数据。您可以在项目中创建相同的模型,然后使用NewtonsfotJson库将JSON字符串脱盐到适当的模型并在使用数据绑定后。

But there is better way for work with JSON data. You can create same models in your project and then usig NewtonsfotJson library desalinize JSON string to proper Model and after use data binding .


这篇关于[UWP] [C#]使用JSON中的图像更改评级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆