Xamarin.Forms перезагрузить коллекцию - PullRequest
0 голосов
/ 20 марта 2020

Я использую Xamarin Forms, и я создал представление коллекции, я получаю данные из API после получения местоположения устройства, мой вопрос, как я могу перезагрузить представление коллекции? Вот мой код:

public partial class HomePage : ContentPage
    {

        APIClass aPIClass = new APIClass();

        public HomePage()
        {

            InitializeComponent();

            getLocation();

        }

        public async void getLocation()
        {
            try
            {
                var location = await Geolocation.GetLastKnownLocationAsync();

                if (location != null)
                {

                    HomeClass places = aPIClass.getUserPlace(location.Latitude.ToString(), location.Longitude.ToString());

                    HomeCollection.ItemsSource = places.results;

                    Console.WriteLine($"Latitude: {location.Latitude}, Longitude: {location.Longitude}, Altitude: {location.Altitude}");
                }
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message.ToString());
            }
        }
    }

И мой взгляд:

<CollectionView x:Name="HomeCollection">
            <CollectionView.ItemsLayout>
               <GridItemsLayout Orientation="Vertical" />
            </CollectionView.ItemsLayout>
            <CollectionView.ItemTemplate>
                <DataTemplate>
                        <Grid Padding="10">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="150" />
                                <RowDefinition Height="35" />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <BoxView Color="White" Grid.Column="1" Grid.RowSpan="2"/>
                            <ContentView Padding="10" Grid.Row="0" Grid.Column="1">
                                <Image Grid.Row="0" Grid.Column="1" Source="{Binding photos[0].name}" Aspect="AspectFill" />
                            </ContentView>
                            <ContentView Padding="10" Grid.Row="1" Grid.Column="1">
                                <Label Grid.Row="1"
                                       Grid.Column="1" 
                                       Text="{Binding name}" />
                            </ContentView>
                        </Grid>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>

Занятые классы:

public class HomeClass
    {
        public List<PlacesClass> results { get; set; }

    }


public class PlacesClass
    {
        public string name { get; set; }
        public List<PlacesImageClass> photos { get; set; }
    }


public class PlacesImageClass
    {
        public int height { get; set; }
        public List<string> html_attributions { get; set; }
        public string photo_reference { get; set; }
        public int width { get; set; }
        public string name { get; set; }
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...