简体   繁体   中英

ListView on Xamarin Forms only shows data when you resave the xml page… Any idea?

Well, I'm working on a Project on Xamarin forms where I drag data from SQL through an API, which is connected to my application. The API works fine, it shows the information from SQL, and then on the ViewModel.cs and I can see the data con my ListView ItemSource={binding path} , but when the process is completed it doesn't show any data or info. It only displays once I hit Ctrl+s on my xaml page.

ListView

<ListView ItemsSource="{Binding CompraCoba}" SeparatorVisibility="Default" SelectionMode="None" MinimumHeightRequest="100" HeightRequest="325" HasUnevenRows="False">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>

                <Grid RowSpacing="10">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="3*" />
                        <ColumnDefinition Width="3*" />
                        <ColumnDefinition Width="3*" />
                    </Grid.ColumnDefinitions>

                    <Label Margin="10,10,10,0" HorizontalOptions="Start" x:Name="SucDlr" Text="{Binding monedaC}" FontSize="10" VerticalOptions="Start" TextColor="White"  Grid.Column="0" HorizontalTextAlignment="Start" />
                    <Label Margin="10,10,10,0" HorizontalOptions="Center" x:Name="MontoDlr" Text="{Binding valorC}" TextColor="White" FontSize="10" VerticalOptions="Start"  Grid.Column="1" />
                    <Label Margin="10,10,10,0" HorizontalOptions="End" x:Name="CambioDlr" Text="{Binding diaC}" TextColor="White" FontSize="10"  VerticalOptions="Start"  Grid.Column="2" />
                    <!--<Label Grid.ColumnSpan="4" BackgroundColor="#bababa" HeightRequest="1" HorizontalOptions="FillAndExpand" VerticalOptions="End" />-->

                </Grid>

            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Code-behind where vm = to my ViewModel. The GetCoba is an http request, it works fine

public ComprasTaxa()
{
    InitializeComponent();
    BindingContext = vm;
    vm.GetCoba(1);
}

Any suggestion would be an excellent help. Thanks in regards

ViewModel

public async void GetCoba(int dias)
        {
            using (var client = new HttpClient())
            {
                var accessToken = Xamarin.Forms.Application.Current.Properties["token"];
                //Get Request
                var url = "https://masterxserver.azurewebsites.net/api/CompraTaxa/GetCoba?dia=" + dias + "";
                client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);
                var result = await client.GetStringAsync(url);
                var CompraTaxaList = JsonConvert.DeserializeObject<List<CompraTaxaM>>(result);


                CompraCoba = new ObservableCollection<CompraTaxaM>(CompraTaxaList);
            }
        }
        ObservableCollection<CompraTaxaM> _compraCoba;

        public ObservableCollection<CompraTaxaM> CompraCoba
        {
            get
            {
                return _compraCoba;
            }
            set
            {
                _compraCoba = value;

                OnPropertyChanged();
            }
        }```

From your code and the description of the issue, it seems that the CompraCoba set is not notifying the View about the changed property. You mentioned that the VM implemented INotifyPropertyChanged , could you please show that code?

Also, it's possible that after you set value for the CompraCoba property, something can override, I would add a Debug.WriteLine and printing the value to be assigned and (important) thread it's assigned from ( Thread.CurrentThread.ManagedThreadId ) and make sure you get this print only once and only from the main thread (#1). The fact that the data is loaded on Ctr+S means that property content is most likely correct but the thread it's been assigned from (or PropertyChanged event) is not.

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