Xamarin - PopAsync()后刷新列表

时间:2017-06-07 07:19:19

标签: c# xamarin xamarin.forms

我正在使用Xamarin表单编写应用程序。当我离开编辑列表的页面时,使用popAsync(),我想刷新上一页的列表,以便显示我的更改。

我的PopulateMachineSelectionList()将我的machine个对象添加到列表中。

这是我到目前为止所尝试的

    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;

    }

3 个答案:

答案 0 :(得分:2)

尝试类似下面的代码:

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

可能会触发propertychanged事件。

答案 1 :(得分:1)

如果您有一个页面A(使用ListView)和一个页面B(编辑绑定到ListView的列表),我认为您可以将pageAViewModel(应该有" list")传递给pageB,并修改它。您应该将更改自动更新为PageA(如果使用ObservableCollection和INPC)。

否则您可以使用MessagingCenter。在Pop之前发送消息从B发送到A"订阅"再次设置ItemsSource

答案 2 :(得分:1)

确保从主线程中调用machineList.ItemsSource =

它也可能有助于使原始ItemSource无效并将新的ItemSource分配给新的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;

}