当我第一次加载我的视图时,我执行以下代码来设置 GridView 源:
ObservableCollection<Categories> categories = await TargetUtils.GetCategoriesRemoteAsync(year);
collectionTarget.Source = categories;
一切都很好,直到我尝试通过添加一些新项目来更新我的网格视图,因为在互联网上找到了很多教程,我使用了 ObservableCollection 并实施了 INotifyPropertyChanged 来更新它。
我的 INotifyPropertyChanged 实现:
public class Model : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
我的类别类:
public class Categories : Model
{
public String Type { get; set; }
public List<MetaDados> MetaDados { get; set; }
public Categories(String Type)
{
this.Type = Type;
}
}
public class MetaDados : Model
{
//NORMAL IMPLEMENTATION
}
并更新我做:
await TargetUtils.GetParcellableCategoriesRemoteAsync(Constants.FULL, year, (ObservableCollection<Categories>)collectionTarget.Source);
调用:
public static async Task<ObservableCollection<Categories>> GetParcellableCategoriesRemoteAsync(String type, int year, ObservableCollection<Categories> categories)
{
foreach (Categories c in categories)
{
if (c.Type.Equals(type))
{
c.MetaDados = await LoadMetaDadosTask.loadMetaDados(type, year, c.MetaDados);
}
}
return categories;
}
最后:
public static async Task<List<MetaDados>> loadMetaDados(String type, int year, List<MetaDados> metaDados)
{
//MY UPDATE CODE AND:
metaDados.Add(PARAMETERS);
return metaDados;
}
如果我使用 ObservableCollection 并实现 INotifyPropertyChanged,我不知道为什么我的 gridView 不更新,非常感谢一些帮助。