Space
Space

Reputation: 144

Initialize an xamarin view after an async method

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

Upvotes: 0

Views: 203

Answers (1)

Cherry Bu - MSFT
Cherry Bu - MSFT

Reputation: 10346

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

Upvotes: 1

Related Questions