GridView没有';t在Windows RT中更新

本文关键字:Windows RT 更新 没有 GridView | 更新日期: 2023-09-27 18:29:53

当我第一次加载视图时,我会执行以下代码来设置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不更新,非常感谢您的帮助。

GridView没有';t在Windows RT中更新

正如Sanket已经提到的,当属性值发生变化时,您需要调用OnPropertyChanged。最好的地方是在属性设置器内部,例如

private String _type;
public String Type 
{ 
    get
    {
        return _type;
    }
    set
    {
        _type = value;
        OnPropertyChanged("Type");
    }
}

这样,每次为属性设置新值时,都会引发PropertyChanged事件。

除此之外,我认为Categories.MetaDados属性最好使用ObservableList<MetaDados>而不是List<MetaDados>,因为您正在向现有列表中添加新项目,并且ObservableCollection<T>实现的INotifyCollectionChanged可以通知GridView。由于只有集合中的项目项目才会更改,而不是整个集合,因此更改也会更流畅地呈现。

此外,否则甚至可能发生GridView将忽略Categories.MetaDados上的PropertyChanged,因为已经分配了已经呈现的列表的相同实例。

仅实现INotifyPropertyChanged对您没有帮助。

您还需要提高ProeprtyChagned活动。否则,绑定将不知道有更改。

一旦您将Categories加载到ObservableCollection上,就可以调用Raise Property Change事件。