Xamarin.Forms从其他页面更新列表视图

qfe3c7zg  于 2023-02-06  发布在  其他
关注(0)|答案(2)|浏览(149)

我有两个页面...主页包含一个列表视图和一个按钮,弹出一个页面(第二页)插入到精简版。所以我有一个load()方法,从精简版数据库中获取数据并将其加载到列表视图。我在主页构造函数中调用此方法,并在每次插入后的第二页中调用此方法。
主页中的加载方法

public void load_Records()
        {

            ObservableCollection<MyChickenModal> chickens_record;
            using (var db = new DataAccess())
            {
                chickens_record = new ObservableCollection<MyChickenModal>(db.GetAllData());

            }
            DisplayAlert("Alert", chickens_record.Count.ToString(), "OK");

            record_lst.ItemsSource = chickens_record;

        }

而第二页

public partial class NewChicksPopUp : Rg.Plugins.Popup.Pages.PopupPage
    {

        public DateTime selected_date = DateTime.Today.Date;
        MainPage mainP;

        public NewChicksPopUp (MainPage mainP)
        {
            InitializeComponent ();
            this.mainP = mainP;
        }

        private void chickenSubmit_btn_Clicked(object o, EventArgs e)
        {

            string record_name = record_name_txt.Text;

            using (var db = new DataAccess())
            {
                if (!string.IsNullOrEmpty(record_name) && !string.IsNullOrWhiteSpace(record_name) && selected_date!=null)
                {

                    db.InsertChicken(new MyChickenModal { Id = 1, Name = record_name, Age = 0, Weight = 0, StartDate = selected_date.ToString("dd/MM/yyyy") });

                }
            }

//************ HERE I CALL THE LOAD METHOD FROM MAIN PAGE********
            mainP.load_Records();

            PopupNavigation.Instance.PopAsync(true);

        }

        private void closePop_btn_Clicked(object o, EventArgs e)
        {

            PopupNavigation.Instance.PopAsync(true);

        }

        private void record_date_picker_DateSelected(object sender , DateChangedEventArgs e)
        {
            selected_date = e.NewDate.Date;
        }


    }

问题是,load方法在构造函数中工作正常,并将数据加载到列表视图中...当我在插入后从另一个页面调用load方法时,该方法工作正常,因为显示了警报消息,但列表根本没有改变。注意:插入是确定的,因为当我重新运行应用程序,新的记录出现

fjnneemd

fjnneemd1#

试试这个:

public void load_Records()
{
    ObservableCollection<MyChickenModal> chickens_record;
    using (var db = new DataAccess())
    {
        chickens_record = new ObservableCollection<MyChickenModal>(db.GetAllData());

    }
    DisplayAlert("Alert", chickens_record.Count.ToString(), "OK");

    // Setting the ItemsSource to null will refresh the ListView
    record_lst.ItemsSource = null;

    record_lst.ItemsSource = chickens_record;

}

这是/是一个变通办法,在早期的单声道做的伎俩。我不确定这是否仍然有效,但值得一试

MVVM方式

正如我已经提到的,用MVVM的方式来做,添加一个名为xxxViewModel的新类:

public class YourPageViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    private ObservableCollection<MyChickenModal> _chicken;
    public ObservableCollection<MyChickenModal> Chicken
    {
        get => _chicken;
        set
        {
            _chicken = value;
            PropertyChanged(this, new PropertyChangedEventArgs(nameof(Chicken)));
        }
    }

    public void LoadRecords()
    {
        var collection = new ObservableCollection<MyChickenModal>();

        using (var db = new DataAccess())
        {
            var allData = db.GetAllData();
            collection = new ObservableCollection<MyChickenModal>(allData);

        }
        DisplayAlert("Alert", chickens_record.Count.ToString(), "OK");

        Chicken = collection;
    }
}

您的Xaml代码隐藏:

public partial class YourPage
{
    public YourPage ()
    {
        InitializeComponent ();

        // Set the ViewModel
        this.BindingContext = new YourPageViewModel();
    }
}

在Xaml中,您可以这样做:

<ListView ... ItemsSource="{Binding Chicken}" .../>
iaqfqrcu

iaqfqrcu2#

从添加页面返回后重新加载主页面上的列表视图。在主页面上设置列表视图出现时的项目源

protected override void OnAppearing()
{
    base.OnAppearing();

    toDoListView.ItemsSource = App.Database.GetItems();
}

相关问题