我正在开发一个Xamarin应用程序,我正在尝试将JSON项目放入列表中,但这些项目不会出现在我的ListView中。 我试着按照一些教程,视频课程,并尝试按照自己的Xamarin项目,但没有成功。
我在这个项目中使用MVVM设计模式。
我正在提供代码,请您告诉我忘记在列表中显示该项目的位置。
ClinicaPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="CartaoDeTodos.View.ClinicaPage">
<StackLayout>
<ListView
x:Name="ClinicaList"
ItemsSource="{Binding Clinicas}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Grid.Padding>
<OnPlatform x:TypeArguments="Thickness">
<OnPlatform.iOS>
10,5,10,5
</OnPlatform.iOS>
<OnPlatform.Android>
10,5,10,5
</OnPlatform.Android>
<OnPlatform.WinPhone>
20,25,20,25
</OnPlatform.WinPhone>
</OnPlatform>
</Grid.Padding>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackLayout Grid.Column="1"
Padding="8"
Spacing="4"
VerticalOptions="Center">
<Label Text="{Binding Nome}" FontSize="Medium" TextColor="#3498DB" LineBreakMode="NoWrap"/>
</StackLayout>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button x:Name="btnVoltar" Text="Voltar" Command="{Binding VoltarCommnad}"></Button>
</StackLayout>
</ContentPage>
ClinicaPage.Xaml.cs
public partial class ClinicaPage : ContentPage
{
public ClinicaPage()
{
InitializeComponent();
var viewModel = new ClinicaViewModel(this);
BindingContext = viewModel;
viewModel.GetClinicasCommad.Execute(null);
}
}
ClinicaViewModel.cs
public class ClinicaViewModel : ViewModelBase
{
private ICommand _voltarCommand;
public ObservableCollection<Clinica> Clinicas { get; set; }
public ClinicaViewModel(Page page) : base(page)
{
Clinicas = new ObservableCollection<Clinica>();
}
private Command getClinicasCommand;
public Command GetClinicasCommad
{
get
{
return getClinicasCommand ??
(getClinicasCommand = new Command(async () => await ExecuteGetClinicasCommand()));
}
}
private async Task ExecuteGetClinicasCommand()
{
var clinicaServico = new ClinicaServico();
GetClinicasCommad.ChangeCanExecute();
var clinicas = await clinicaServico.ListagemClinicaAsync();
}
public ICommand VoltarCommnad
{
get
{
return _voltarCommand ?? (_voltarCommand = new Command(() =>
{
page.Navigation.PushModalAsync(new LoginPage());
}));
}
}
}
ClinicaServico.cs
public class ClinicaServico
{
private const string MyChesysApi = "Oculto";
public async Task<JsonRetornoApi<List<Clinica>>> ListagemClinicaAsync()
{
var client = new RestClient(MyChesysApi);
var request = new RestRequest("/api//Clinica/ClinicasVinculadas", Method.GET);
string token = (string) Application.Current.Properties["access_token"];
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", "Bearer "+token);
IRestResponse response = await client.Execute(request);
var clinicas = JsonConvert.DeserializeObject<JsonRetornoApi<List<Clinica>>>(response.Content);
if (!response.IsSuccess)
{
throw new InfraException("Serviço fora ou não encontrado.");
}
if (response.StatusCode == HttpStatusCode.Unauthorized)
{
throw new RegraNegocioServicoException("Sessão expirada, faça login novamente.");
}
return clinicas;
}
}
JsonRetornoApi.cs
public class JsonRetornoApi<T> where T : class
{
public JsonRetornoApi()
{
}
public int CodigoRetorno { get; set; }
public string Mensagem { get; set; }
public T Dados { get; set; }
}
Clinica.cs
public class Clinica
{
public string Nome { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
}
JSON
{"CodigoRetorno":0,"Mensagem":"Sucesso","Dados":[{"Bairro":"Vila Ana Maria","CEP":"14026220","CNES":null,"CNPJCPF":"65625656256256","Complemento":null,"Email":null,"Endereco":"Rua Raul Peixoto","Municipio":"Ribeirão Preto","Nome":"Viper Clinicas","Numero":"630","UF":"SP","HoraInicio":"08:00:00","HoraFim":"20:00:00","Latitude":"-21.2103565","Longitude":"-47.8183606","Codigo":6259,"Excluido":false},{"Bairro":"Centro","CEP":"14015040","CNES":null,"CNPJCPF":"01190190000150","Complemento":null,"Email":null,"Endereco":"Rua São Sebastião","Municipio":"Ribeirão Preto","Nome":"Clínica Independência","Numero":"1442","UF":"SP","HoraInicio":"07:00:00","HoraFim":"21:00:00","Latitude":"-21.1812446","Longitude":"-47.8030487","Codigo":7244,"Excluido":false}]}
答案 0 :(得分:1)
看起来您永远不会使用从REST调用中获得的项目更新ObservableCollection。
private async Task ExecuteGetClinicasCommand()
{
var clinicaServico = new ClinicaServico();
GetClinicasCommad.ChangeCanExecute();
var clinicas = await clinicaServico.ListagemClinicaAsync();
// Update your observable collection with clinicas here
foreach (var clinica in clinicas)
Clinicas.Add(clinica);
}