0
votes

I'm using:

  • VS 2019
  • Xamarin 16.1.0.545
  • Xamarin Forms 4.1.0

I have a Xamarin Android project that was working before and after updating VS, ListView bindings stopped working.

I have a Content page with this listview:

<ListView x:Name="ListViewServicos" SelectionMode="None" ItemTapped="ListViewServicos_ItemTapped">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell Height="80">
                <StackLayout Orientation="Vertical" Padding="10" Spacing="2" HeightRequest="80">
                    <StackLayout Orientation="Horizontal" VerticalOptions="StartAndExpand">
                        <Image Source="arrow" HorizontalOptions="Start"></Image>
                        <Label Text="{Binding Tipo}" TextColor="Black" HorizontalOptions="StartAndExpand"/>
                        <StackLayout Orientation="Horizontal" HorizontalOptions="EndAndExpand" WidthRequest="100">
                            <Label Text="{Binding MargemTotalFormatada}" TextColor="#555555" HorizontalOptions="EndAndExpand"/>
                        </StackLayout>
                    </StackLayout>
                    <StackLayout Orientation="Horizontal" VerticalOptions="StartAndExpand">
                        <StackLayout Orientation="Horizontal" HorizontalOptions="StartAndExpand">
                            <Label Text="Uso:" TextColor="#555555" HorizontalOptions="StartAndExpand"/>
                            <Label Text="{Binding MargemUtilizadaFormatada}" TextColor="#555555" HorizontalOptions="EndAndExpand"/>
                        </StackLayout>
                        <StackLayout Orientation="Horizontal" HorizontalOptions="EndAndExpand">
                            <Label Text="Disponível:" TextColor="#555555" HorizontalOptions="StartAndExpand" FontAttributes="Bold"/>
                            <Label Text="{Binding MargemFormatada}" TextColor="#555555" HorizontalOptions="EndAndExpand" FontAttributes="Bold"/>
                        </StackLayout>
                    </StackLayout>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

I set the ItemsSource in the .cs like this (in the LoadServicesList method called inside OnAppearing):

protected override void OnAppearing()
{
    base.OnAppearing();
    BindingContext = this;
    ConfigureScreen();
    LoadServicesList();
    servico = User.Servicos.First();
    LoadService(servico);
}

public void LoadServicesList()
{
    ListViewServicos.RowHeight = 80;
    ListViewServicos.HeightRequest = User.Servicos.Count() * 80;
    ListViewServicos.ItemsSource = User.Servicos;
}

User is static and 'Servicos' is a List of 'Servico':

public static class User
    {
        public static List<Servico> Servicos { get; set; }

And here is the 'Servico' class:

public class Servico
{
    public string Tipo { get; set; }
    public double Margem { get; set; }

    public string MargemFormatada { get; set; }
        
    public double MargemUtilizada { get; set; }

    public string MargemUtilizadaFormatada { get; set; }

    public string MargemTotalFormatada { get; set; }

    public string Color { get; set; }

    public List<Margem> Margens { get; set; }

    public Servico(string tipo, double margem, double margemUtilizada)
    {
        this.Tipo = tipo;
        Margem = margem;
        MargemUtilizada = margemUtilizada;
        MargemFormatada = string.Format(new CultureInfo("pt-BR"), "{0:C}", margem);
        MargemUtilizadaFormatada = string.Format(new CultureInfo("pt-BR"), "{0:C}", margemUtilizada);
        MargemTotalFormatada = string.Format(new CultureInfo("pt-BR"), "{0:C}", margem + margemUtilizada);
    }
}

The weirdest part of all this is that only the "Tipo" property is rendered, the other binding just don't show up (yes, I've checked and they have values).

I've been searching for a solution for a long time but nothing worked for me so far. I really appreciate if anyone can help me. Thanks!

Ps: I've already tried adding "BindingContext = this;" after InitializeComponent() but nothing changed.

UPDATE:

I just found out that only "Tipo" property of Servico was working because in other part of the code I was calling User.Servicos.First().Tipo. It looks like it will only show up in the listview binding if the property "get" was called before. That's super weird.

So the workaround is to loop through User.Servicos and call all properties I will use in Listview:

string x = servico.MargemTotalFormatada;
string y = servico.MargemUtilizadaFormatada;
string w = servico.MargemFormatada;

With that code, ListView bindings work. That is super ugly and wrong, but that's how I've made it work.

1
please do NOT post code and errors as images! It is impossible to read/copy/paste/index and therefore makes it difficult to help youJason
Thank you @Jason, just updated and added the code properly.tksask

1 Answers

1
votes

1.change your List<Servico> Servicos to ObservableCollection<Servico> Servicos:

public static class User
{
    public static ObservableCollection<Servico> Servicos { get; set; }
}

2.call LoadServicesList after other methods.

    protected override void OnAppearing()
    {
        base.OnAppearing();
        BindingContext = this;
        ConfigureScreen();
        servico = User.Servicos.First();
        LoadService(servico);

        LoadServicesList();

    }

Refer: listview/data-and-databinding

Update:

Sample project here: data-binding-of-Listview