0

Good evening everyone.

For some time now I have been to Xamarin. My first tests are rather conclusive. I decided to try to make a small application that retrieves information in a database via an API and then update this data via a ListView. When I launch the application on my emulator everything works but as soon as I install the application on my phone it crashes. I thought this was because the API but I have an API that I use to check the Login / password that works correctly. The API that returns the data reviews a lot of line about 3500/4000, can this be the reason?

So I passed the loading of the data in my viewModel in an async method but the problem now is that the view loads before the data is loaded correctly. Is there a way to get the view initialized after the data is loaded?

Below my code.

Initializing my viewModel

class ManageInventViewModel
{
    public ObservableCollection<InventViewModel> InventProduct { get; set; }

    public Command<InventViewModel> UpdateCommand
    {
        get
        {
            return new Command<InventViewModel>(invent =>
            {
                var index = invent.IndexLigneInventaire;
                InventProduct.Remove(invent);
                InventProduct.Insert(index, invent);
            });
        }
    }

    public Command<InventViewModel> ResetStock
    {
        get
        {
            return new Command<InventViewModel>(invent =>
            {
                var index = InventProduct.IndexOf(invent);
                InventProduct.Remove(invent);
                invent.RealStockProduct = 0;
                InventProduct.Insert(index, invent);
            });
        }
    }

    public ManageInventViewModel()
    {
        LoadInventaire();
    }

    private async void LoadInventaire()
    {
        var listMvt = await Utils.Utils.GetListMouvementUntilDate();
        var listStock = Utils.Utils.GetStockByProduct(listMvt).Take(20);

        InventProduct = new ObservableCollection<InventViewModel>();

        var indexLine = 0;
        foreach (var stock in listStock)
        {
            var inventViewModel = new InventViewModel
            {
                LibelleProduit = stock.LibelleProduit,
                PrCodeProduit = stock.PrCodeProduit,
                UpCodeProduit = stock.UpCodeProduit,
                RealStockProduct = stock.StockTheoProdct,
                StockTheoProdct = stock.StockTheoProdct,
                IndexLigneInventaire = indexLine
            };
            ++indexLine;
            InventProduct.Add(inventViewModel);
        }
    }
}

Initializinz my view

public partial class InventPage : ContentPage
{
    public InventPage()
    {
        InitializeComponent();

        TableInvent.ItemSelected += (sender, e) =>
        {
            if (TableInvent.SelectedItem != null)
            {
                if (TableInvent.SelectedItem is InventViewModel item)
                {
                    PopupNavigation.Instance.PushAsync(new ChangeStockModal(item, this));
                }

                TableInvent.SelectedItem = null;
            }
        };
    }

    private void Reset_Stock(object sender, EventArgs e)
    {
        var input = sender as Button;
        var inventViewModel = input?.BindingContext as InventViewModel;
        var listViewModel = BindingContext as ManageInventViewModel;
        listViewModel?.ResetStock.Execute(inventViewModel);
    }

    public void Update_Stock_List(InventViewModel dataStockUpdate)
    {
        var listViewModel = BindingContext as ManageInventViewModel;
        listViewModel?.UpdateCommand.Execute(dataStockUpdate);
        PopupNavigation.Instance.PopAsync();
    }
}

Thanks

4
  • 1
    generally if you need to load data async, it is a GOOD idea to display the UI first with a spinner or dialog that indicates something is happening. Delaying the UI until after the data loads just makes the user think the app is doing nothing and is broken Commented Jan 17, 2019 at 19:20
  • 1
    Why not use an ActivityIndicator while the data is being loaded? After that, just show the view correctly. You can't pause a view loading Commented Jan 17, 2019 at 19:20
  • Thank you for your answers I will try to set up the ActivityIndicator and see if it works. I'll come back and ask if it's not working properly. Commented Jan 17, 2019 at 19:24
  • I am encountering a problem. I managed to create the ActivityIndicator but I can not get my data loaded while I'm displaying the wait screen. And once the data load how to send them to my initialization page of my ListView? Commented Jan 17, 2019 at 19:44

1 Answer 1

1

I managed to create the ActivityIndicator but I can not get my data loaded while I'm displaying the wait screen.

Regarding this issue, I don't see you useActivityIndicator from your code,maybe you didn't update your code, I think if you use useActivityIndicator , You can bind one property to ActivityIndicator IsRunning and IsVisible, then you can solve your issue. Related use ActivityIndicator step, you can take a look:

ActivityIndicator

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.