Beispiel #1
0
        void SpreadSheetViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Authorized")
            {
                if (Authorized)
                {

                    _database = new SpreadSheetDB(GoogleOAuth2LoginViewModel.OAuth2);

                    ThreadStart start = delegate()
                    {

                        ObservableCollection<BookViewModel> sheets = null;
                        try
                        {

                            Synchronizing = true;
                            _database.Init();

                            sheets = _database.RetrieveSpreadsheet();

                        }
                        catch (System.Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }

                        if (Application.Current != null)
                        {
                            Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal,
                                                       new Action(() =>
                                                       {
                                                           if (sheets != null)
                                                           {
                                                               // deep copy of collection's items created from different thread
                                                               foreach (var bookitem in sheets)
                                                               {
                                                                   BookViewModel book = new BookViewModel(_database, bookitem.BookTitle, bookitem.Entry);
                                                                   book.Book.CollectionChanged += book.VocaViewModel_PropertyChanged;

                                                                   foreach (var vocaitem in bookitem.Book)
                                                                   {
                                                                       book.Book.Add(vocaitem);
                                                                   }

                                                                   this.SpreadSheet.Add(book);
                                                               }

                                                               // move focus to the last
                                                               ICollectionView vs = CollectionViewSource.GetDefaultView(this.SpreadSheet);
                                                               vs.MoveCurrentToLast();
                                                           }

                                                           Synchronizing = false;
                                                       }));

                        }

                    };

                    new Thread(start).Start();

                }
                else
                {

                }
            }
        }