[FIXED] Can't add to a list from async function

Issue

i try to learn Xamarin.Forms and async methods. But since this morning i’m stuck :(.
I can’t add a single string to my list. I used stop point on every line of my function (the first i will send) and every variable as what it should have inside. But for a reason i don’t understand these data won’t add to my lists. The problem most likely come from my approximative comprehension of async methods.

My goal is to create some presets for my small app. The user can create a preset by pressing a button and is saved as a text file with the name the user provided via an Entry(Xamarin). I want to use a Picker(Xamarin) to select the preset to use. But he stay empty except for the one i declared by hand in the MainPage constructor.

I’m using Xamarin.Forms and PCLstorage.

My lists declaration:

public List<IFile> presetFilesList = new List<IFile>();
public List<string> presetNamesList = new List<string>();

The call for my function:

public MainPage()
    {
        InitializeComponent();

        //set preset picker
        presetNamesList.Add("Choose a preset here");
        var test = GetAllfilesInSiRoDataAsync();
    }

and here is the function:

private async Task<bool> GetAllfilesInSiRoDataAsync()
    {
        bool result = false;
        await Task.Run(async() =>
        {
            IFolder root = PCLStorage.FileSystem.Current.LocalStorage;
            IFolder folder = await root.GetFolderAsync("SiRoData", System.Threading.CancellationToken.None);
            var siroFolder = await folder.GetFilesAsync();

            foreach (IFile file in siroFolder)
            {
                presetFilesList.Add(file);
                string nomFichier = file.Name;
                nomFichier = nomFichier.Remove(nomFichier.Length - 4);
                presetNamesList.Add(nomFichier);
            }

            presetPicker.ItemsSource = presetNamesList;
            presetPicker.SelectedIndex = 0;
            result = true;
            });
        return result;
    }

Thanks for reading this ! 🙂

Solution

For it to work i first changed my variable declaration, removed the "Names" list (for the names displayed in the picker i now use the IFile property: Name (filename)).

Code:

        public IList<IFile> presetFilesList;

I also changed the way to fetch the files, i stopped trying to do everything in the GetAllGetAllfilesInSiRoDataAsync() function (it’s easier to use now). This function just go in the folder takes all the files in it and return an IList of IFile.

Code:

private static async Task<IList<IFile>> GetAllfilesInSiRoDataAsync()
    {
        IFolder root = PCLStorage.FileSystem.Current.LocalStorage;
        IFolder folder = await root.GetFolderAsync("SiRoData", System.Threading.CancellationToken.None);
        var siroFolder = await folder.GetFilesAsync();
        return siroFolder;
    }

I now call this function on the OnAppearing() function (as jason said it). In this function i simply assign the return of GetAllFiles() to a new temporary list:

Code:

protected override async void OnAppearing()
    {
        IList<IFile> list = await GetAllfilesInSiRoDataAsync();

        PassTo(list);

    }

Since i could not pass my List of IFiles from an async function to my presetFilesList (declared on the first step). i created a function to do it.

Then i pass this list to the PassTo() function that takes IList of IFile
as a parameter.

Code:

        private void PassTo(IList<IFile> presetsList)
    {
        presetFilesList = presetsList;
        presetPicker.ItemsSource = (System.Collections.IList)presetFilesList;
    }

I also made some change in my xaml: i created a Binding for ItemSource to my list. After that i set ItemDisplayBinding to be Name so the value my picker displays is the Name property of the Ifile.

<Picker x:Name="presetPicker" VerticalOptions="Center" HorizontalOptions="FillAndExpand" TextColor="White" HorizontalTextAlignment="Center" Margin="0, 0, 30, 0" SelectedIndexChanged="presetPicker_SelectedIndexChanged" ItemsSource="{Binding presetFilesList}" ItemDisplayBinding="{Binding Name}" Title="Choose a preset" TitleColor="White"/>

Thanks for the help 🙂 ,sorry for the bad english.

Answered By – Zartox29

Answer Checked By – Dawn Plyler (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published