简体   繁体   中英

Xamarin C# PCL - json content to listview

I'm learning Xamarin and was trying to push this Jira Json output to a listview named ListView1. If anyone would like to help a newbie please by all means. Everything works up to outputting data in debug, when i output the content but does not go to the list view. What am I doing wrong?

Note I had to use DataContract for Avatar URLS because it was bringing up invalid when ran in json2sharp. not sure if thats correct.

Here is a sample of the Json OUTPUT https://0bin.net/paste/8vbm-XlwIPcuxCeL#qG-rL5BzxIzCNx/3LpUdO8VMukLuFiAvG7+wHdNbra5 which I made into a Class, (Working with Jira output)

public class ItemClass
    {
        public class Links
        {
            public string @base { get; set; }
            public string context { get; set; }
            public string self { get; set; }
        }

        public class CreatedDate
        {
            public string iso8601 { get; set; }
            public string jira { get; set; }
            public string friendly { get; set; }
            public object epochMillis { get; set; }
        }

        [DataContract]
        public class AvatarUrls
        {
            [DataMember(Name = "48x48")]
            public string Icon_48x48 { get; set; }
            [DataMember(Name = "24x24")]
            public string Icon_24x24 { get; set; }
            [DataMember(Name = "16x16")]
            public string Icon_16x16 { get; set; }
            [DataMember(Name = "32x32")]
            public string Icon_32x32 { get; set; }
        }

        public class Links2
        {
            public string jiraRest { get; set; }
            public AvatarUrls avatarUrls { get; set; }
            public string self { get; set; }
        }

        public class Reporter
        {
            public string name { get; set; }
            public string key { get; set; }
            public string emailAddress { get; set; }
            public string displayName { get; set; }
            public bool active { get; set; }
            public string timeZone { get; set; }
            public Links2 _links { get; set; }
        }

        public class RequestFieldValue
        {
            public string fieldId { get; set; }
            public string label { get; set; }
            public object value { get; set; }
        }

        public class StatusDate
        {
            public string iso8601 { get; set; }
            public string jira { get; set; }
            public string friendly { get; set; }
            public object epochMillis { get; set; }
        }

        public class CurrentStatus
        {
            public string status { get; set; }
            public StatusDate statusDate { get; set; }
        }

        public class Links3
        {
            public string web { get; set; }
            public string self { get; set; }
        }

        public class Value
        {
            public List<string> _expands { get; set; }
            public string issueId { get; set; }
            public string issueKey { get; set; }
            public string requestTypeId { get; set; }
            public string serviceDeskId { get; set; }
            public CreatedDate createdDate { get; set; }
            public Reporter reporter { get; set; }
            public List<RequestFieldValue> requestFieldValues { get; set; }
            public CurrentStatus currentStatus { get; set; }
            public Links3 _links { get; set; }
        }

        public class RootObject
        {
            public List<string> _expands { get; set; }
            public int size { get; set; }
            public int start { get; set; }
            public int limit { get; set; }
            public bool isLastPage { get; set; }
            public Links _links { get; set; }
            public List<Value> values { get; set; }
        }

    }

Code when executed to display:

using (HttpClient client = new HttpClient())
            {
                var content = "";
                string url = "https://***/rest/servicedeskapi/request";
                client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", baseauth);
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

                HttpResponseMessage response = await client.GetAsync(url);
                content = await response.Content.ReadAsStringAsync();

                var Items = JsonConvert.DeserializeObject<List<ItemClass.RootObject>>(content);
                ListView1.ItemsSource = Items;

Listview on xaml:

<ListView x:Name="ListView1" RowHeight="60">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout Orientation="Vertical" Padding="8,0,8,0">
                                <Label Text="{Binding reporter}" TextColor="#000" FontSize="14" LineBreakMode="TailTruncation" />
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

Your binding to a List<RootObject> . And in your Cell you bind to the reporter property. The RootObject however, does not have a reporter property. So either add the reporter to it or, looking at your code, make the ItemsSource : Items.values , which has the reporter property.

So change your code like this:

var Items = JsonConvert.DeserializeObject<List<ItemClass.RootObject>>(content);
ListView1.ItemsSource = Items.values;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM