简体   繁体   中英

data from firebase to listView in xamarin forms

I have this Task which bring data from firebase:

public async Task<List<Player>> GetPlayersData()
        {
            var playersData = (await client.Child("Players")
                .OnceAsync<Player>())
                .Select(f => new Player
                {
                    PlayerName = f.Object.PlayerName,
                    PlayerTime = f.Object.PlayerTime,
                    PlayerRank = f.Object.PlayerRank
                }).ToList();
            return playersData;
        }

it worked perfectly, but i want to get the (playersData) to put it as "ItemSource" in "Listview" in XAML, so i call the task in the page constructor:

public RankTable()
        {
            InitializeComponent();
            BindingContext = this;
            client = new FirebaseClient("https://numbergame-*****firebaseio.com/");

            GetPlayersData();
        }  

Now it will run the task and brings the players data but I don't know how to control it and put it as ItemSource in the Following XAML code:

<StackLayout>

            <CollectionView Grid.ColumnSpan="2" Grid.RowSpan="2" SelectionMode="None" ItemsSource="{Binding ??}">
            <CollectionView.ItemTemplate>
                <DataTemplate>

                        <Frame HasShadow="True" BackgroundColor="Beige" CornerRadius="15"
                               IsClippedToBounds="True" Visual="Material" InputTransparent="True">
                        <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                            
                        </Grid.ColumnDefinitions>
                        <Label Text="{Binding PlayerRank}" FontSize="15" Grid.Column="1"/>
                        <Label Text="{Binding PlayerName}" FontSize="15" Grid.Column="2"/>
                        <Label Text="{Binding PlayerTime}" FontSize="15" Grid.Column="3"/>
                        
                        </Grid>
                        </Frame>
                    </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
        </StackLayout>

any help please and sorry for my bad english..

<CollectionView x:Name="Players" ... /

// GetPlayersData is async so you can't call it from the constructor
protected override void OnAppearing()
{
    Players.ItemsSource = await GetPlayersData();
}

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