2016-12-14 70 views
0

我有一个需求列表,每个需求都具有我从json中提取的属性,目前我认为我只对来自JSON的typeName和requisiteStatusId属性感兴趣。这两个是与该需求类型(考试,阅读文章等)相对应的图像的属性及其当前状态。如何在我的列表视图中显示图片?

我已经可以显示列表视图中的图像,但我必须证明对应的typeName和requisiteStatusId的图像,例如

{ 
    "id": 1221, 
    "name": "Visibility - Public Acknowledgement", 
    "description": "Acknowledgement from Omar during a Q Meeting", 
    "typeId": 11, 
    **"typeName": "Proven XP",** 
    "validationId": null, 
    "skillId": 131, 
    "skillName": "Visibility", 
    "material": "No Material", 
    "materialUrl": "#", 
    **"requisiteStatusId": 4,** 
    "trailName": "General Knowledge", 
    "trailId": 5, 
    "levelId": 1  
}, 

这两个,你必须显示相应的图像相关,有三种状态和10种类型,现在我直接显示这样的图像,例如:

public class CustomVeggieCell : ViewCell 
{ 
    public CustomVeggieCell() 
    { 
     var image = new Image 
     { 

     }; 
     var image2 = new Image 
     { 
     }; 
     var nameLabel = new Label 
     { 

      VerticalTextAlignment = TextAlignment.Center, 
      TextColor = Color.FromHex("#FF9E9E9E"), 
     }; 

     var horizontalLayout = new StackLayout() 
     { 

     }; 
     //set bindings 

     nameLabel.SetBinding(Label.TextProperty, "Name", BindingMode.TwoWay); 


     image.Source = "book.png"; 
     image2.Source = "Palomitashida.png"; 
     //Set properties for desired design 
     horizontalLayout.Orientation = StackOrientation.Horizontal; 

     image2.HorizontalOptions = LayoutOptions.EndAndExpand; 
     nameLabel.HorizontalOptions = LayoutOptions.StartAndExpand; 
     nameLabel.VerticalOptions = LayoutOptions.Center; 
     nameLabel.TextColor = Color.FromHex("#FF9E9E9E"); 
     //add views to the view hierarchy 
     horizontalLayout.Children.Add(image); 
     horizontalLayout.Children.Add(nameLabel); 
     horizontalLayout.Children.Add(image2); 

     View = horizontalLayout; 


    } 
} 

我不知道如何从JSON中提取的属性或如何验证它使使用id来提取JSON的告诉我是什么类型和状态的要求,我离开他们的图像,以便它可以从从的NuGet可here在上下文中 enter image description here

+0

所以你想从JSON或ListView.SmallImageList和LargeImageList中提取值? –

+0

@MohitShrivastava我想提取,“typeName”和“requisiteStatusId”,因为我需要显示图像,根据属性,以及现在我需要知道如何显示图像依赖于json –

回答

0

使用NewtonSoft JSON多一点。您可以通过在解决方案资源管理器>管理NuGet包中右键单击您的项目,将Visual Studio中的NuGet包添加到您的项目中。

下面是我对你的要求创建一个类:

public class Requirement 
{ 
    public int id { get; set; } 
    public string name { get; set; } 
    public string description { get; set; } 
    public int typeId { get; set; } 
    public string typeName { get; set; } 
    public object validationId { get; set; } 
    public int skillId { get; set; } 
    public string skillName { get; set; } 
    public string material { get; set; } 
    public string materialUrl { get; set; } 
    public int requisiteStatusId { get; set; } 
    public string trailName { get; set; } 
    public int trailId { get; set; } 
    public int levelId { get; set; } 
} 

您可以使用下面的代码NewtonSoft反序列化JSON是这样的:

var item = JsonConvert.DeserializeObject<Requirement>("JSON String goes here"); 

这将一个需要工作。您可能会拥有一组JSON对象或其他一些结构,根据您的结构调整代码。

您现在可以使用上面反序列化的item来读取它的值。

对于一个快速测试,如果你把你的json(没有最后一个逗号和你添加的**)放到一个文本文件中,并将文本文件放到bin/debug文件夹中,那么你可以得到JSON像这样反序列化:

var item = JsonConvert.DeserializeObject<Requirement>(File.ReadAllText("Json.txt")); 
+0

我不需要反序列化,我可以提取本地变量中的json例如: UserPathContract _userPathContract = new UserPathContract(); _userPathContract = Application.Current.Properties [“UserTrail”]作为UserPathContract; VAR要件= _userPathContract.Requisites 。凡(R => r.TrailId == _currentTrail.Id && r.LevelId == _currentLevel.Id && r.SkillId == skill.Id && r.RequisiteStatusId == _currentRequisiteStatus。 ID); –

+0

它为我在验证选择器改变勇气,我想知道如何提取属性验证必要条件,并显示相应的图像 –

+0

好吧,你可以从反序列化的JSON中提取属性,然后使用属性显示正确的图像。如果我正确理解您的问题,那么JSON将具有您想要使用的值。 – CodingYoshi