Hello everyone!
I'm trying to consume a REST API and update the Text property of some Entry views. But as soon as I get a JSON response and set some properties of my POCO class the view it's not updating.
Take a look in my code:
ViewModel:
namespace MyApp.ViewModels
{
public class CompanyInfoPageViewModel : ViewModelBase //which inherits BindableBase
{
private APIClient _apiClient;
public CompanyInfoPageViewModel(
INavigationService navigationService,
IPageDialogService pageDialogService,
APIClient apiClient) : base(navigationService, pageDialogService)
{
_apiClient = apiClient;
Company = new Company();
}
private Company company;
public Company Company
{
get { return company; }
set { SetProperty(ref company, value); }
}
//Fired on TextChanged of txtCEP Entry
public async void FindCEP(string text)
{
var response = await _apiClient.GetAsync("cep/" + text);
if (response.IsSuccessStatusCode)
{
var json = await response.ToJObjectAsync();
var error = json.Value<string>("errors");
if (error == null)
{
var data = json["data"];
Company.Endereco = json["data"]["log_no_abrev"].ToString();
Company.Bairro = json["data"]["bairro"].ToString();
Company.Cidade = json["data"]["cidade"].ToString();
//And the view it's not updated ;(
}
else
{
await PageDialogService.DisplayAlertAsync("Ops", error, "OK");
}
}
else
{
//displays another message
}
}
}
public class Company
{
public string Endereco { get; set; }
public string Bairro { get; set; }
public string Cidade { get; set; }
public string CEP { get; set; }
}
}
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"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
prism:ViewModelLocator.AutowireViewModel="True"
x:Class="MyApp.Views.CompanyInfo"
Title="Dados da empresa">
<StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="Center">
<StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="Center" Padding="0,20,0,0">
<Label Text="Endereço" FontSize="Large"/>
<Entry Text="{Binding Company.CEP}" Placeholder="CEP" x:Name="txtCEP" />
<Entry Text="{Binding Company.Endereco, Mode=TwoWay }" Placeholder="Rua, Av..." HorizontalOptions="FillAndExpand"/>
<Entry Text="{Binding Company.Bairro}" Placeholder="Bairro" />
<Entry Text="{Binding Company.Cidade}" Placeholder="Cidade" HorizontalOptions="FillAndExpand"/>
</StackLayout>
<Button Command="{Binding SalvarCommand}" Text="Salvar" Style="{StaticResource buttonPrimary}"/>
</StackLayout>
</ContentPage>
Am I doing something wrong?