Can anyone please tell me why I can't see the Tarefa.Condutor text variable in a label in my app? The Label in the app appears empty. What am I doing wrong? I think it's because of the Text="{Binding Tarefa.Condutor}" that doesn't binds correctly.
I have this code in .xaml file:
ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="GesfrotaTarefasApp.MVVM.Views.DashboardPage"
xmlns:vm="clr-namespace:GesfrotaTarefasApp.MVVM.ViewModels"
Title="DashboardPage">
<ContentPage.BindingContext>
<vm:DashboardViewModel />
</ContentPage.BindingContext>
<StackLayout BackgroundColor="White">
<Button x:Name="botao" Text="Carrega dados" Command="{Binding GetTarefaFakeCommand}" CommandParameter="{Binding CommandParam}"></Button>
<Label x:Name="Teste" Text="{Binding Tarefa.Condutor}" FontSize="14" TextColor="Black"></Label>
</StackLayout>
</ContentPage>
And this code in .xaml.cs:
public partial class DashboardPage : ContentPage
{
IConnectivity connectivity;
public DashboardPage()
{
InitializeComponent();
BindingContext = new DashboardViewModel();
}
}
And this code in ViewModel.cs:
public partial class DashboardViewModel : BaseViewModel
{
HttpClient httpClient;
private int commandParam = 1;
public int CommandParam
{
get
{
return commandParam;
}
set
{
SetProperty(ref commandParam, value);
}
}
private TarefaDTO tarefa;
public TarefaDTO Tarefa
{
get
{
return tarefa;
}
set
{
SetProperty(ref tarefa, value);
}
}
public DashboardViewModel()
{
Title = "Dashboard";
httpClient = new HttpClient();
tarefa = new TarefaDTO();
}
public async Task<List<TarefaDTO>> GetTarefaFromFake()
{
return await Task.FromResult(new List<TarefaDTO>
{
new TarefaDTO(){ Cliente = "Joao", Condutor = "Luis", DataCriacao = DateTime.Now,
Estado = "Activo", IdTarefaEstado = 1, IdCondutor = 1, Id = 1 }, new TarefaDTO(){ Cliente = "Nuno", Condutor = "Fabio", DataCriacao = DateTime.Now, Estado = "Activo", IdTarefaEstado = 2, IdCondutor = 2, Id = 2 } , new TarefaDTO(){ Cliente = "Marta", Condutor = "Luisa", DataCriacao = DateTime.Now, Estado = "Activo", IdTarefaEstado = 3, IdCondutor = 3, Id = 3 }
});
}
[RelayCommand]
public async Task<TarefaDTO> GetTarefaFake(int? idTarefa)
{
var listaTarefas = await GetTarefaFromFake();
var novatarefa = listaTarefas.Where(t => t.Id == idTarefa).SingleOrDefault();
return novatarefa;
}
And my model is here:
public class TarefaDTO
{
public int Id { get; set; }
public string Cliente { get; set; }
public string Condutor { get; set; }
public string Estado { get; set; }
public DateTime DataCriacao { get; set; }
public int IdCondutor { get; set; }
public int IdTarefaEstado { get; set; }
}