Xamarin - PopAsync() 之后刷新列表

问题描述 投票:0回答:4

我正在使用 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;

    }
c# xamarin xamarin.forms
4个回答
3
投票

尝试类似以下代码:

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

这可能会触发

propertychanged
事件。


1
投票

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

否则您可以使用 MessagingCenter。在 Pop 之前从 B 向 A 发送一条消息,然后在“订阅”上再次设置您的 ItemsSource


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;

}

0
投票

将此添加到第一页:

NavigatedTo += async (s, e) => { await Refresh(); };
© www.soinside.com 2019 - 2024. All rights reserved.