2016-12-14 1 views
0

필자는 요구 사항 목록을 가지고 있으며 각 요구 사항에는 JSON에서 추출한 속성이 있으며 잠시 JSON의 typeName 및 requisiteStatusId 속성에만 관심이 있다고 생각합니다. 이 두 가지는 해당 요구 사항 유형 (시험, 기사 읽기 등) 및 현재 상태에 해당하는 이미지의 속성입니다. ,내 listview에 사진을 표시하려면 어떻게해야합니까?

나는 이미 목록보기 내부 이미지를 표시 할 수 있습니다하지만 난 당신이 해당 이미지를 표시해야 할 두 가지에 의존 예를

{ 
    "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  
}, 

를 들어, 유형 이름과 requisiteStatusId에 해당하는 이미지를 표시해야 세 개의 주와 10 종류의 예를 들어, 지금은이 방법으로 직접 이미지를 보여주는거야 들어있다 : 나는 JSON에서 속성을 추출하거나하는 방법의 유효성을 검사하는 방법을 모르는

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; 


    } 
} 

이 Nuget에서 here에서 컨텍스트 enter image description here

+0

그래서 JSON 또는 ListView.SmallImageList 및 LargeImageList에서 값을 추출하고 싶습니까? –

+0

@MohitShrivastava 속성에 따라 쇼 이미지가 필요하기 때문에 "typeName"및 "requisiteStatusId"를 추출하고 싶습니다. 이제는 json에 종속 된 이미지를 표시하는 방법을 알고 있어야합니다. –

답변

0

사용 NewtonSoft JSON에서 좀 더 될 수 있도록 그렇게 json으로는 요구 사항이 무엇인지 유형과 상태를 말해에서 추출하기 위해 ID를 사용하는 것을, 나는 그들에게 이미지를 둡니다. 솔루션 탐색기> Manage NuGet Packages에서 프로젝트를 마우스 오른쪽 버튼으로 클릭하여 Visual Studio에서 프로젝트에 NuGet 패키지를 추가 할 수 있습니다.

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

이 하나 개의 요구 사항과 함께 작동합니다 : 당신은 NewtonSoft와 함께이 같은 JSON을 역 직렬화 아래의 코드를 사용할 수 있습니다

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; } 
} 

: 여기

난 당신의 요구 사항을 위해 만든 클래스입니다 . 구조에 따라 코드를 조정할 수 있도록 JSON 객체 또는 다른 구조의 배열이있을 것입니다.

이제 item을 역 직렬화하여 값을 읽을 수 있습니다.

마지막 쉼표와 **를 추가하지 않고 json을 텍스트 파일에 넣은 다음 텍스트 파일을 bin/debug 폴더에 넣으면 빠른 테스트를 위해 JSON을 얻을 수 있습니다 다음과 같이 deserialized :

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

deserialize가 필요하지 않습니다. 로컬 변수에서 json을 추출 할 수 있습니다. 예 : UserPathContract _userPathContract = new UserPathContract(); _userPathContract = Application.Current.Properties [ "UserTrail"]을 (를) UserPathContract로; VAR 용구 용의 _userPathContract.Requisites = 어디에요는 (R => r.TrailId ==는 _currentTrail.Id && r.LevelId ==는 && r.SkillId == skill.Id && r.RequisiteStatusId == _currentRequisiteStatus을 _currentLevel.Id. 신분증); –

+0

그것은 picker 변경 용기에서 유효성 검사를 위해, 나는 requiesite를 확인하고 이미지를 표시하는 방법을 알고 싶다. –

+0

에 해당하는 이미지를 잘 표시하면 deserialized json에서 속성을 추출한 다음 올바른 이미지를 표시하기 위해 속성을 사용할 수있다. 귀하의 질문을 올바르게 이해하면 JSON에 사용할 값이 생깁니다. – CodingYoshi

관련 문제