简体   繁体   中英

xamarin.forms display on listview

I want to display the Name and Logo onto the List view however no result seems to display it out. Is there any way that I can pass this error and display my results? There is an error message when I try to debug the problem on the setClubs().

<ListView  x:Name="Clublistviews">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout BackgroundColor="White"
                    Orientation="Vertical">
                        <StackLayout Orientation="Horizontal">
                            <Image Source="{Binding Logo}" IsVisible="true" WidthRequest="42" HeightRequest="42"/>
                             <Button Text="{Binding Name}" x:Name="BtnClub" AbsoluteLayout.LayoutBounds="100, 25, 100, 25" Clicked="OnListClubsClicked" TextColor="Black" VerticalOptions="FillAndExpand"/>
                        </StackLayout>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

My xaml.cs codes are

    public ListClubs ()
    {
        InitializeComponent();
        setClubs(); //The statement is not awaited and execution of current method continues before the call is complete. Consider using 'await' operator or calling 'Wait' method  
    }

    public IEnumerable<CsClubList> ClubList 
    {
        get { return _ClubList; }
        set { _ClubList = value; }
    }

    private async Task setClubs()
    {
        ClubApiClient service = new ClubApiClient();
        var clublist = await service.getClubList();
        Clublistviews.ItemsSource = clublist.ToList();
    }

Avoid using async method in your constructor.

In your page.cs (where you added Clublistviews), override OnAppearing, and call await Clublistviews.setClubs().

protected override async void OnAppearing()
{
    await Clublistviews.setClubs();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM