我在 Visual Studio 中使用基于模板的 GroupedItemsPage。提供了一个 SampleDataSource,因此我以它为例并创建了自己的数据源。但是,我想将从 Internet 异步下载的项目添加到网格视图中。问题是,一旦我得到结果并处理它们并将它们添加到数据源,屏幕不会更新以显示新的网格页面。
这是我LoadState
在 GroupedItemsPage 类上的方法:
protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
{
// TODO: Create an appropriate data model for your problem domain to replace the sample data
var h = DataModel.MainPageDataSource.GetSingleton().GetGridGroups("AllGroups");
this.DefaultViewModel["Groups"] = h;
}
这是我的数据源类:
class MainPageDataSource : FacepunchWin8.Common.BindableBase
{
public static MainPageDataSource _singleton = new MainPageDataSource();
public static MainPageDataSource GetSingleton() { return _singleton; }
private static Uri _baseUri = new Uri("ms-appx:///");
private ObservableCollection<MainPageGrid> _gridGroups = new ObservableCollection<MainPageGrid>();
void IndexParserComplete(HTMLParser p)
{
HTMLParserIndex parser = p as HTMLParserIndex;
foreach (ForumSection f in parser.GetForumSections())
{
MainPageGrid forumGrid = new MainPageGrid(f.Title(), f.Title());
_gridGroups.Add(forumGrid);
}
}
private void ItemsCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
}
public MainPageDataSource()
{
HTMLParserIndex parser = new HTMLParserIndex();
parser.SetComplateDelegate(IndexParserComplete);
Scraper.GetSingleton().RequestForumIndex(parser);
MainPageGrid Grid1 = new MainPageGrid("GRID1", "TestPage");
Grid1.Items.Add(new MainPageGridItem("Grid00", "GridOne", "Subtitle", "Assets/DarkGray.png"));
_gridGroups.Add(Grid1);
}
public ObservableCollection<MainPageGrid> GetGridGroups(string uniqueId)
{
if (!uniqueId.Equals("AllGroups")) throw new ArgumentException("Only 'AllGroups' is supported as a collection of groups");
return _gridGroups;
}
}
构造函数创建了一些很像 的项目SampleDataSource
,它们在屏幕上正确显示。但是,从互联网上下载数据后,IndexParserComplete
会调用 _gridGroups 中的一些新项目。如何通知 GroupedItemsPage 刷新屏幕并从数据源重新读取数据?