0

Visual Studio のテンプレートに基づいた GroupedItemsPage を使用しています。SampleDataSource が提供されているので、それを例として使用し、独自のデータ ソースを作成しました。ただし、インターネットから非同期的にダウンロードされるアイテムをグリッド ビューに追加したいと考えています。問題は、結果を取得して処理し、データ ソースに追加すると、画面が更新されず、新しいグリッド ページが表示されないことです。

LoadStateGroupedItemsPage クラスのメソッドは次のとおりです。

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 に通知して画面を更新し、データ ソースからデータを再読み込みするにはどうすればよいですか?

4

1 に答える 1

0

やっと答えがわかった。別のスレッドからの ObservableCollection の変更/別のスレッドからのデータ ソースの設定はサポートされていません。非同期操作からデータを追加していたので、Dispatcher を使用して、メイン UI スレッドを使用してデータを追加する必要がありました。

_gridGroups.Add(forumGrid);私は次のIndexParserComplete(HTMLParser p)ものに置き換えました:

 this._uiDispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
               {
                    DataModel.MainPageDataSource.GetSingleton().AddPageGrid(forumGrid);
               })

where _uiDispatcher = Windows.UI.Core.CoreWindow.GetForCurrentThread().Dispatcher;

于 2013-09-20T21:49:08.483 に答える