Skip to content

Instantly share code, notes, and snippets.

@brunoportess
Last active February 16, 2018 13:03
Show Gist options
  • Save brunoportess/9417144c2b838ae0e0852da8c19091d2 to your computer and use it in GitHub Desktop.
Save brunoportess/9417144c2b838ae0e0852da8c19091d2 to your computer and use it in GitHub Desktop.
BindingContext ViewModel Sample
namespace DocHouse.Models.Entities
{
public class Servico
{
public int Id { get; set; }
public string NomeServico { get; set; }
public string Imagem { get; set; }
public double Valor { get; set; }
}
}
<StackLayout>
<ActivityIndicator IsRunning="{Binding IsBusy}" IsVisible="{Binding IsBusy}"/>
<Label Text="Carregando..." IsVisible="{Binding IsBusy}" HorizontalOptions="Center" TextColor="White"/>
<controls:FlowListView
HorizontalOptions="CenterAndExpand"
x:Name="flowListView"
FlowColumnCount="2"
RowHeight="150"
FlowUseAbsoluteLayoutInternally="False"
FlowColumnExpand="None"
FlowItemTappedCommand="{Binding ServicoCommand}"
FlowItemsSource="{Binding ListaServicos}" >
<controls:FlowListView.FlowColumnTemplate>
<DataTemplate>
<StackLayout
Padding="10"
HorizontalOptions="CenterAndExpand"
VerticalOptions="CenterAndExpand">
<Image
HeightRequest="100"
WidthRequest="100"
Source="{Binding Imagem}"></Image>
<Label
FontSize="Medium"
TextColor="Black"
HorizontalTextAlignment="Center"
Text="{Binding NomeServico}"/>
</StackLayout>
</DataTemplate>
</controls:FlowListView.FlowColumnTemplate>
</controls:FlowListView>
</StackLayout>
namespace DocHouse.Views.Consulta
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Consulta1Servicos : ContentPage
{
private readonly ServicosViewModel _servicosVm;
public Consulta1Servicos()
{
InitializeComponent();
_servicosVm = this.BindingContext = new ServicosViewModel();
}
}
}
namespace DocHouse.ViewModels.Consulta
{
public class Consulta1ServicosViewModel : BaseViewModel
{
public DelegateCommand<Servico> ServicoCommand { get; set; }
private readonly INavigationService _navigationService;
private readonly IPageDialogService _dialogService;
private readonly RestConsulta _restConsulta;
private List<Servico> _listaServicos;
public List<Servico> ListaServicos
{
get => _listaServicos;
set => SetProperty(ref _listaServicos, value);
}
private bool _isBusy;
public bool IsBusy
{
get { return _isBusy; }
set { SetProperty(ref _isBusy, value); }
}
public Consulta1ServicosViewModel(INavigationService navigationService, IPageDialogService dialogService)
{
_navigationService = navigationService;
_dialogService = dialogService;
ServicoCommand = new DelegateCommand<Servico>(ExecuteServicoCommand);
ListaServicos = new List<Servico>();
_restConsulta = new RestConsulta();
Dados();
}
private async void ExecuteServicoCommand(Servico obj)
{
var p = new NavigationParameters { { "servico", obj } };
await _navigationService.NavigateAsync("Consulta2Prestadores", p);
}
private async void Dados()
{
try
{
IsBusy = true;
var list = await _restConsulta.GetServices();
ListaServicos = list;
}
catch (System.Exception ex)
{
Debug.WriteLine(ex.Message);
IsBusy = false;
throw;
}
finally
{
IsBusy = false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment