【发布时间】:2018-11-08 18:10:22
【问题描述】:
我一直在关注关于将我的 xamarin 表单应用程序连接到 asp.net web api 的 youtube 教程。不幸的是,我的 Listview 没有被来自 api 的数据填充。
Xamarin 表单应用具有以下文件:
RestClient.cs
public class RestClient<T> {
private const string WebServiceUrl = "http://localhost:49864/api/Oppotunities/";
public async Task<List<T>> GetAsync()
{
var httpClient = new HttpClient();
var json = await httpClient.GetStringAsync(WebServiceUrl);
var OppotunityList = JsonConvert.DeserializeObject<List<T>>(json);
return OppotunityList ;
} }
MainViewModel.cs
public MainViewModel()
{
InitializeDataAsync();
}
private async Task InitializeDataAsync()
{
var oppotunitiesServices = new OppotunitiesServices();
OppotunitiesList = await oppotunitiesServices.GetOppotunitiesAsync();
}
OppotunityServices.cs
public class OppotunitiesServices
{
public async Task<List<Oppotunity>> GetOppotunitiesAsync()
{
RestClient<Oppotunity> restClient = new RestClient<Oppotunity >();
var oppotunitiesList = await restClient.GetAsync();
return oppotunitiesList;
}
}
【问题讨论】:
-
您是否正在使用模拟器进行任何更改?如果是这样,您不应该使用 localhost 连接到您的环境
-
是的。我正在使用模拟器。赦免。请问我可以用什么
-
使用运行您的 Web 服务的机器的实际 IP 地址。像 http://192.168.0.101:49864/api/Oppotunities/
标签: c# asp.net xamarin.forms