xamarin 在加载页面之前填充ObservableCollection时,ItemsSource不显示/bind

js81xvg6  于 2023-01-22  发布在  其他
关注(0)|答案(1)|浏览(157)

我有一个carouselview,在那个视图中我有一个ObservableCollection绑定为itemssource,我可以绑定这个集合,当我在OnAppearing事件中执行视图模型的命令时,它会显示出来。
有效的代码:

    • 第二页**
public partial class SecondPage : ContentPage
{

     public Coll(bool hard, string subject)
     {
            InitializeComponent();
            var vm = (DataSelectionViewModel)BindingContext;

            vm.Hard = hard;
            vm.Subject = subject;
            /* had to set "hard" and "subject" here again, otherwise data won't load */
     }

     protected override async void OnAppearing()
     {
         var vm = (DataSelectionViewModel)BindingContext;
         base.OnAppearing();
         await vm.LoadData.ExecuteAsync().ConfigureAwait(false);
     }
}
    • 第二页的视图模型**
public class DataSelectionViewModel : BaseViewModel
{
   private string subject;
   public string Subject { get => subject; set => SetProperty(ref subject, value); }

   private bool hard;
   public bool Hard { get => hard; set => SetProperty(ref hard, value); }

   public ObservableCollection<Items> FilteredData { get; set; }

   public UserSelectionViewModel()
   {
            _dataStore = DependencyService.Get<IDataStore>();

            LoadData= new AsyncAwaitBestPractices.MVVM.AsyncCommand(FilterData);

            FilteredData = new ObservableCollection<Items>();
   }
   public async Task FilterData()
   {
            FilteredData.Clear();
            var filtereddata = await _dataStore.SearchData(Hard, Subject).ConfigureAwait(false);
            foreach (var data in filtereddata)
            { 
                FilteredData.Add(data);
            }
   }  
}
    • 第一页,其中第二页获取"困难"和"主题"值**
private async void ButtonClick(object sender, EventArgs e)
{
   var vm = (BaseViewModel)BindingContext;

   vm.Hard = HardButtonSelected == Hard;
   vm.Subject = vm.Subject.ToLower();

   await Navigation.PushAsync(new SecondPage(vm.Hard, vm.Subject));
   
}

所以我想修改我的代码,这样如果我按下第一页上的按钮,数据就会立即开始过滤并添加到ObservableCollection中,完成后,导航到第二页,但是如果我尝试将其加载到BaseViewModel中,然后从第二个视图模型中获取数据,它不会显示数据。
无法工作的代码:

    • 第二页**
public partial class SecondPage : ContentPage
{

     public SecondPage()
     {
            InitializeComponent();
     }
}
    • 第二页的视图模型**
public class DataSelectionViewModel : BaseViewModel
{

   public ObservableCollection<Items> FilteredData { get; set; }

   public UserSelectionViewModel()
   {

            FilteredData = new ObservableCollection<Items>();
   }
}
    • 基本视图模型**
public class BaseViewModel : INotifyPropertyChanged
{
   private string subject;
   public string Subject { get => subject; set => SetProperty(ref subject, value); }

   private bool hard;
   public bool Hard { get => hard; set => SetProperty(ref hard, value); }

   public ObservableCollection<Items> FilteredData { get; set; }

   /* BaseViewModel has implementation of SetProperty */
}
    • 第一页,其中第二页获取"困难"和"主题"值**
private async void ButtonClick(object sender, EventArgs e)
{
   var vm = (BaseViewModel)BindingContext;

   vm.Hard = HardButtonSelected == Hard;
   vm.Subject = vm.Subject.ToLower();
}
    • 首页视图模型**
public class FirstPageViewModel : BaseViewModel
    {

        public IAsyncCommand MehetButtonClickedCommand { get; }
        readonly IPageService pageService;
        readonly IFeladatokStore _feladatokStore;
        public FeladatValasztoViewModel()
        {
            _dataStore = DependencyService.Get<IDataStore>();
            ButtonClickedCommand = new AsyncCommand(ButtonClicked);

            pageService = DependencyService.Get<IPageService>();
        }

        private async Task ButtonClicked()
        {
            await FilterData();

            await pageService.PushAsync(new SecondPage());
        }

        private async Task FilterData()
        {
            FilteredData.Clear();
            var datas = await _dataStore.SearchData(Subject, Hard).ConfigureAwait(false);
            foreach (var data in datas)
            {
                FilteredData.Add(data);
            }
        }

基本上这会产生一个null异常错误,我也试过把ObservableCollection作为SecondPage(ObservableCollection x)的一个参数,这确实起作用了,但是因为我必须为它创建另一个ObservableCollection,并从一个复制到另一个,它停止了异步,并冻结了几秒钟,所以我的问题是,我怎么才能让它异步呢?

pieyvz9o

pieyvz9o1#

为了避免延迟,在私有变量中构建新集合。* 然后 * 将属性设置为该变量:

// Constructor with parameter
public SomeClass(IList<Items> data)
{
    SetFilteredDataCopy(data);
}

public ObservableCollection<Items> FilteredData { get; set; }

private void SetFilteredDataCopy(IList<Items> src)
{
    var copy = new ObservableCollection<Items>();
    foreach (var item in src)
        copy.Add(item);
    FilteredData = copy;
    //MAYBE OnPropertyChanged(nameof(FilteredData));
}

相关问题