[英]Xamarin C# PCL - json content to listview
我正在學習Xamarin,並試圖將Jira Json的輸出推送到名為ListView1的列表視圖中。 如果有人想幫助新手,請務必。 當我輸出內容但不進入列表視圖時,一切都會在調試中輸出數據。 我究竟做錯了什么?
請注意,我必須對Avatar URL使用DataContract,因為在json2sharp中運行時它顯示無效。 不知道那是正確的。
這是Json OUTPUT https://0bin.net/paste/8vbm-XlwIPcuxCeL#qG-rL5BzxIzCNx/3LpUdO8VMukLuFiAvG7+wHdNbra5的示例,我將其分類(使用Jira輸出)
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; }
}
}
執行時顯示的代碼:
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;
xaml上的Listview:
<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>
您綁定到List<RootObject>
。 在您的Cell
您綁定到reporter
屬性。 但是, RootObject
沒有reporter
屬性。 因此,可以向其中添加reporter
者,或者查看您的代碼,使ItemsSource
: Items.values
具有reporter
屬性。
因此,像這樣更改代碼:
var Items = JsonConvert.DeserializeObject<List<ItemClass.RootObject>>(content);
ListView1.ItemsSource = Items.values;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.