英文:
Replace items in list for items from FirebaseDatabase data
问题
我可以帮你翻译以下代码部分:
原文:
var GetItems = (await fc
            .Child("Verhaal")
            .OnceAsync<StudentModel>()).Select(item => new StudentModel
{            
Adres = item.Object.Adres,
Plaats = item.Object.Plaats
}).ToList(); // had to add this .ToList() to make the answer from Jessie to work.
 // Change it in to this 
StudentModel obj = new StudentModel();
obj.Adres = "test@gmail.com";
obj.Plaats = "Test1";
_allStudentList.Add(obj);
翻译后:
var GetItems = (await fc
            .Child("Verhaal")
            .OnceAsync<StudentModel>()).Select(item => new StudentModel
{
    Adres = item.Object.Adres,
    Plaats = item.Object.Plaats
}).ToList(); // 不得不添加 .ToList() 以使 Jessie 的答案有效。
// 更改为以下内容
StudentModel obj = new StudentModel();
obj.Adres = "test@gmail.com";
obj.Plaats = "Test1";
_allStudentList.Add(obj);
英文:
Found a project that loads items in a CollectionView from a ViewModel for InfiniteScrolling.
But i want to replace the items with data from a FirebaseDatabase.
How do i make this work ?
There are 25 items in the ViewModel in AddStudentInfo()
    private async void AddStudentInfo()
    {
        StudentModel obj = new StudentModel();
        obj.Plaats = "Test1";
        obj.Adres = "test@gmail.com";
        _allStudentList.Add(obj);
        obj = new StudentModel();
        obj.Plaats = "Test2";
        obj.Adres = "test@gmail.com";
        _allStudentList.Add(obj);
        obj = new StudentModel();
        obj.Plaats = "Test3";
        obj.Adres = "test@gmail.com";
        _allStudentList.Add(obj);
        obj = new StudentModel();
        obj.Plaats = "Test4";
        obj.Adres = "test@gmail.com";
        _allStudentList.Add(obj);
        obj = new StudentModel();
        obj.Plaats = "Test5";
        obj.Adres = "test@gmail.com";
        _allStudentList.Add(obj);
        obj = new StudentModel();
        obj.Plaats = "Test6";
        obj.Adres = "test@gmail.com";
        _allStudentList.Add(obj);
        obj = new StudentModel();
        obj.Plaats = "Test7";
        obj.Adres = "test@gmail.com";
        _allStudentList.Add(obj);
        obj = new StudentModel();
        obj.Plaats = "Test8";
        obj.Adres = "test@gmail.com";
        _allStudentList.Add(obj);
        obj = new StudentModel();
        obj.Plaats = "Test9";
        obj.Adres = "test@gmail.com";
        _allStudentList.Add(obj);
        obj = new StudentModel();
        obj.Plaats = "Test10";
        obj.Adres = "test@gmail.com";
        _allStudentList.Add(obj);
        obj = new StudentModel();
        obj.Plaats = "Test11";
        obj.Adres = "test@gmail.com";
        _allStudentList.Add(obj);
        obj = new StudentModel();
        obj.Plaats = "Test12";
        obj.Adres = "test@gmail.com";
        _allStudentList.Add(obj);
        obj = new StudentModel();
        obj.Plaats = "Test13";
        obj.Adres = "test@gmail.com";
        _allStudentList.Add(obj);
        obj = new StudentModel();
        obj.Plaats = "Test14";
        obj.Adres = "test@gmail.com";
        _allStudentList.Add(obj);
        obj = new StudentModel();
        obj.Plaats = "Test15";
        obj.Adres = "test@gmail.com";
        _allStudentList.Add(obj);
        obj = new StudentModel();
        obj.Plaats = "Test16";
        obj.Adres = "test@gmail.com";
        _allStudentList.Add(obj);
        obj = new StudentModel();
        obj.Plaats = "Test17";
        obj.Adres = "test@gmail.com";
        _allStudentList.Add(obj);
        obj = new StudentModel();
        obj.Plaats = "Test18";
        obj.Adres = "test@gmail.com";
        _allStudentList.Add(obj);
        obj = new StudentModel();
        obj.Plaats = "Test19";
        obj.Adres = "test@gmail.com";
        _allStudentList.Add(obj);
        obj = new StudentModel();
        obj.Plaats = "Test20";
        obj.Adres = "test@gmail.com";
        _allStudentList.Add(obj);
        obj = new StudentModel();
        obj.Plaats = "Test21";
        obj.Adres = "test@gmail.com";
        _allStudentList.Add(obj);
        obj = new StudentModel();
        obj.Plaats = "Test22";
        obj.Adres = "test@gmail.com";
        _allStudentList.Add(obj);
        obj = new StudentModel();
        obj.Plaats = "Test23";
        obj.Adres = "test@gmail.com";
        _allStudentList.Add(obj);
        obj = new StudentModel();
        obj.Plaats = "Test24";
        obj.Adres = "test@gmail.com";
        _allStudentList.Add(obj);
        obj = new StudentModel();
        obj.Plaats = "Test25";
        obj.Adres = "test@gmail.com";
        _allStudentList.Add(obj);
  
    }
I can load data with this from a FirebaseDatabase.
  var GetItems = (await fc
          .Child("Verhaal")
          .OnceAsync<StudentModel>()).Select(item => new StudentModel
          {
              Adres = item.Object.Adres,
              Plaats = item.Object.Plaats
          }).ToList(); // had to add this .ToList() to make the answer from Jessie to work.
What do i have to change to make this work. Items from Adres to obj.Adres etc.
 var GetItems = (await fc
        .Child("Verhaal")
        .OnceAsync<StudentModel>()).Select(item => new StudentModel
        {            
        Adres = item.Object.Adres,
        Plaats = item.Object.Plaats
        }).ToList(); // had to add this .ToList() to make the answer from Jessie to work.
         // Change it in to this 
        StudentModel obj = new StudentModel();
        obj.Adres = "test@gmail.com";
        obj.Plaats = "Test1";
        _allStudentList.Add(obj);
答案1
得分: 1
你可以尝试使用async-await来实现这个。
请参考以下的代码:
public ObservableCollection<StudentModel> AllStudentList { get; set; }
AllStudentList = new ObservableCollection<StudentModel>();
private async void LoadData()
{
    AllStudentList = new ObservableCollection<StudentModel>();
    
    //GetItems 应该是一个列表
    var GetItems = (await fc
        .Child("Verhaal")
        .OnceAsync<StudentModel>()).Select(item => new StudentModel
        {
            Adres = item.Object.Adres,
            Plaats = item.Object.Plaats
        });
    foreach (var item in GetItems) {
        AllStudentList.Add(item);
    }
}
希望对你有帮助。
英文:
You can try to use async-await to achieve this.
Please refer to the following code:
   public ObservableCollection<StudentModel> AllStudentList { get; set; }
    AllStudentList = new ObservableCollection<StudentModel>();
    private async  void LoadData()
    {
        AllStudentList = new ObservableCollection<StudentModel>();
        
        //GetItems should be a list
        var GetItems = (await fc
    .Child("Verhaal")
    .OnceAsync<StudentModel>()).Select(item => new StudentModel
    {
        Adres = item.Object.Adres,
        Plaats = item.Object.Plaats
    });
        foreach (var item in GetItems) {
            AllStudentList.Add(item);
        }
    }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论