Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

async task in viewmodel won't run my foreach loop and populate data

$
0
0

Yes, I'm a noob and I have probably a silly question...

I'm populating a listview with my viewmodel and it works fine if I statically assign values, but when I try to run an async task which loops through a list to add the values it does not populate my listview.

Here's the code behind I'm using:
`
public class ViewModel
{
public ViewModel()
{
Task.Run(async () => { await GetCatsAsync(); });
}

    public List<SourceItem> Source { get; set; }

    public async Task GetCatsAsync()
    {
        List<groceryList> itemsT = new List<groceryList>(await App.GroceryRepo.GetAllCategoriesAsync());

        foreach (var groceryList in itemsT)
        {
            if (groceryList.CategoryHead)
            {
                this.Source.Add(new SourceItem(groceryList.Category.ToString()));
            }
        }
    }
}

public class SourceItem
{
    public SourceItem(string name)
    {
        this.Name = name;
    }

    public string Name { get; set; }
}`

And here's my xaml:
`
<ContentPage.BindingContext>
viewModels:ViewModel x:Name="ViewModel"
</ContentPage.BindingContext>

            <telerikDataControls:RadListView x:Name="listViewModelTest" ItemsSource="{Binding Source}" HeightRequest="200">
                <telerikDataControls:RadListView.ItemTemplate>
                    <DataTemplate>
                        <listView:ListViewTemplateCell>
                            <listView:ListViewTemplateCell.View>
                                <Grid>
                                    <Label Margin="10" Text="{Binding Name}" />
                                </Grid>
                            </listView:ListViewTemplateCell.View>
                        </listView:ListViewTemplateCell>
                    </DataTemplate>
                </telerikDataControls:RadListView.ItemTemplate>
            </telerikDataControls:RadListView>`

The list itemsT does retrieve data and it hits my foreach but does not populate my listview.

Any ideas?


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>