Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Three ways to bind a XAML ListView - how to refresh, which is the best?

$
0
0

I have seen three ways to bind a ListView, what is the good way, how to get the ListView refreshed?

1. non static:

XAML:

    <ContentPage.BindingContext>
        <viewModels:PositionsViewModel/>
    </ContentPage.BindingContext>
            <ListView x:Name="listView"
                      SeparatorVisibility="Default"
                      HasUnevenRows = "true"
                      ItemSelected="OnCelestialPositionSelected"
                      ItemsSource="{Binding Positions}">
                <!-- ItemsSource="{x:Static viewModels:PositionsViewModel.Positions}" -->

Model:

    public class PositionsViewModel : BaseViewModel
    {
        public List<Position> Positions { get; set; }

        public PositionsViewModel()
        {
            Positions = App.PositionsSightsDB.GetPositionsAsync().Result;
            Positions.Sort();
        }

2. Static

XAML:

    <ContentPage.BindingContext>
        <viewModels:PositionsViewModel/>
    </ContentPage.BindingContext>
            <ListView x:Name="listView"
                      SeparatorVisibility="Default"
                      HasUnevenRows = "true"
                      ItemSelected="OnCelestialPositionSelected"
                     <!--  ItemsSource="{Binding Positions}"-->
                ItemsSource="{x:Static viewModels:PositionsViewModel.Positions}" >

Model:

    public class PositionsViewModel : BaseViewModel
    {
        public static List<Position> Positions { get; set; }

        public PositionsViewModel()
        {
            Positions = App.PositionsSightsDB.GetPositionsAsync().Result;
            Positions.Sort();
        }

3. OnAppearing:

.xaml.cs

        protected override async void OnAppearing()
        {
            base.OnAppearing();
            listView.ItemsSource = await AlmicantaratXF.App.PositionsSightsDB.GetPositionsAsync();
        }

I don't understand the difference between the 1. (non-static) and the 2. (Static). The list does not refresh: if I PushAsync a page, delete an item and PopAsync() the page which came over

With the 3rd solution, the list is refreshed but not sorted.

How to get a sorted and refreshed list?


Viewing all articles
Browse latest Browse all 77050

Trending Articles



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