我想在按钮名称和图像徽标上的列表视图中显示源的值,因为我已经绑定了图像源和名称。但是似乎没有显示结果。
public async Task<string> GetRequest (string url){
Uri geturi = new Uri("http://172.20.129.193/nypstudentlifeservice/");
HttpClient client = new HttpClient();
HttpResponseMessage responseGet = await client.GetAsync(geturi);
return responseGet.Content.ReadAsStringAsync().Result;
}
public async Task<IEnumerable<CsClubList>> getClubList()
{
string result = await GetRequest("api/club/list?categoryid=2");
CsClubList[] clublist = null;
var clubtoken = JObject.Parse(result)[0];
clublist = clubtoken.Select(clublists => new CsClubList()
{
Id = (int)clublists["Id"],
Name = (string)clublists["Name"],
Logo = (string)clublists["Logo"],
CategoryId = (int)clublists["CategoryId"]
}).ToArray();
return clublist;
}
这是我尝试使用Json Web服务的代码。
这是CsClubList
public class CsClubList
{
public int Id{ get; set;}
public int CategoryId { get; set; }
public string Name {get; set;}
public string Logo {get; set;}
}
对于xaml.cs文件
public partial class ListClubs : ContentPage
{
private IEnumerable<CsClubList> _ClubList;
public ListClubs ()
{
InitializeComponent();
setClubs(Clublistview); //The statement is not awaited and execution of current method continues before the call is complete. Consider using 'await' operator or calling 'Wait' method
}
public IEnumerable<CsClubList> ClubList
{
get { return _ClubList; }
set { _ClubList = value; }
}
private async void setClubs(ListView listview)
{
ClubApiClient service = new ClubApiClient();
var clublist = await service.getClubList();
listview.ItemsSource = clublist.ToList();
}
}
这是xaml页面:
<ListView x:Name="Clublistview">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout BackgroundColor="White"
Orientation="Vertical">
<StackLayout Orientation="Horizontal">
<Image Source="{Binding Logo}" IsVisible="true" WidthRequest="42" HeightRequest="42"/>
<Button Text="{Binding Name}" x:Name="BtnClub" AbsoluteLayout.LayoutBounds="100, 25, 100, 25" Clicked="OnListClubsClicked" TextColor="Black" VerticalOptions="FillAndExpand"/>
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
这是Json Web服务的链接:
http://172.20.129.193/nypstudentlifeservice/api/club/list?categoryid=2
[{"Id":21,"CategoryId":2,"Name":"Chinese Cultural Group","Logo":"http://www.nyp.edu.sg/web/campus_life/cca/arts_&_culture/image/CCG2.png"},{"Id":22,"CategoryId":2,"Name":"Chinese Orchestra","Logo":"http://www.nyp.edu.sg/web/campus_life/cca/arts_&_culture/image/CO-ICON.png"},{"Id":23,"CategoryId":2,"Name":"Dance Company","Logo":"http://www.nyp.edu.sg/web/campus_life/cca/arts_&_culture/image/DC2.png"},{"Id":24,"CategoryId":2,"Name":"Danz Inc.","Logo":"http://www.nyp.edu.sg/web/campus_life/cca/arts_&_culture/image/DI-ICON.png"},{"Id":25,"CategoryId":2,"Name":"Der Treff German Club","Logo":"http://www.nyp.edu.sg/web/campus_life/cca/arts_&_culture/image/DTG-ICON.png"},{"Id":26,"CategoryId":2,"Name":"Foreign Bodies Dance Group","Logo":"http://www.nyp.edu.sg/web/campus_life/cca/arts_&_culture/image/FB-ICON.png"}]
答案 0 :(得分:0)
您还没有真正说明问题是什么或问题是什么,但这里有一些基于您提供的代码的观察结果。
您正在代码中混合异步和同步调用。不要在异步方法中使用.Result
或.Wait
。
从尝试以字符串形式读取响应的行中删除.Result
,并将结果await
移除,就像使用HTTP请求一样。
还注意到您没有正确构建Uri以满足您的要求。您使用基地址创建了一个Uri,但未包含提供的url参数。
public async Task<string> GetRequest(string url) {
Uri getUri = new Uri("http://172.20.129.193/nypstudentlifeservice/" + url);
HttpClient client = new HttpClient();
HttpResponseMessage responseGet = await client.GetAsync(getUri);
responseGet.EnsureSuccessStatusCode(); // Throw if not a success code.
string content = await responseGet.Content.ReadAsStringAsync();
return content;
}
public async Task<IEnumerable<CsClubList>> getClubList() {
string json = await GetRequest("api/club/list?categoryid=2");
List<CsClubList> clublist = JsonConvert.DeserializeObject<List<CsClubList>>(json);
return clublist;
}
除非它们是事件处理程序,否则您还应尽可能在void方法上使用async。
所以,如果可能的话,你需要改变这个
//NO NO NO NO NO
private async void setClubs(ListView listview)
{
ClubApiClient service = new ClubApiClient();
var clublist = await service.getClubList();
listview.ItemsSource = clublist.ToList();
}
到此
private async Task setClubs(ListView listview)
{
ClubApiClient service = new ClubApiClient();
var clublist = await service.getClubList();
listview.ItemsSource = clublist.ToList();
}