Barney Chambers
Barney Chambers

Reputation: 2783

Xamarin - Refresh list after PopAsync()

I am writing an application using Xamarin-forms. When I navigate away from my page which edits a list, using popAsync() , I want to refresh a list on the previous page so that my changes are shown.

my PopulateMachineSelectionList() adds my machine objects to the list.

This is what I've tried so far

    protected override void OnAppearing()
    {

        PopulateMachineSelectionList();
        base.OnAppearing();
    }


    async void PopulateMachineSelectionList()
    { 
        loadedMachines = await machineSync.GetMachines();
        if (loadedMachines.Count() != 0)
        {
            machineSelectionList.Clear();

            foreach (Machine mach in loadedMachines)
            { //I have an archive boolean that determines whether or not machines should be shown
                if (!mach.archived)
                {
                    Console.WriteLine("Adding: " + mach.name + " to the list template");
                    machineSelectionList.Add(new ListTemplate(null, mach.name, true, true));
                }
            }
            Console.WriteLine("Refresh List");
            machineList.ItemsSource = machineSelectionList;

        }
        machineList.SelectedItem = null;

    }

Upvotes: 1

Views: 6433

Answers (4)

Adrien Taieb
Adrien Taieb

Reputation: 1

Add this to the first page :

NavigatedTo += async (s, e) => { await Refresh(); };

Upvotes: 0

Brandon Minnick
Brandon Minnick

Reputation: 15400

Make sure to call machineList.ItemsSource = from the Main Thread.

It also may help to nullify the original ItemSource and assign the new ItemSource to a new List.

protected override void OnAppearing()
{

    PopulateMachineSelectionList();
    base.OnAppearing();
}


async void PopulateMachineSelectionList()
{ 
    loadedMachines = await machineSync.GetMachines();
    if (loadedMachines.Count() != 0)
    {
        machineSelectionList.Clear();

        foreach (Machine mach in loadedMachines)
        { //I have an archive boolean that determines whether or not machines should be shown
            if (!mach.archived)
            {
                Console.WriteLine("Adding: " + mach.name + " to the list template");
                machineSelectionList.Add(new ListTemplate(null, mach.name, true, true));
            }
        }
        Console.WriteLine("Refresh List");
        Device.BeginInvokeOnMainThread(() =>
        {
            machineList.ItemsSource = null;
            machineList.ItemsSource = new List(machineSelectionList);
        });

    }
    machineList.SelectedItem = null;

}

Upvotes: 1

Kedu
Kedu

Reputation: 1370

Try something like, the following code:

machineList.ItemsSource.Clear(); 
machineList.ItemsSource.Add(machineSelectionList);

Possibly this will trigger, the propertychanged Event.

Upvotes: 3

Alessandro Caliaro
Alessandro Caliaro

Reputation: 5768

If you have a page A (with the ListView) and a page B (that edit the list binded to ListView) I think you can pass pageAViewModel (that should have the "list") to pageB, and modify it. You should have your changes automatically updated to PageA (if you use ObservableCollection and INPC).

Otherwise you could use MessagingCenter. Send a Message from B to A before the Pop and on "Subscribe" set your ItemsSource again

Upvotes: 1

Related Questions