示例#1
0
        public void CreateGameDatabase()
        {
            Deployment.Current.Dispatcher.BeginInvoke(async() =>
            {
                List <KeyValue <int, GameData> > Database = null;

                using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (!isoStore.DirectoryExists("/GameLibrary/"))
                    {
                        isoStore.CreateDirectory("/GameLibrary/");
                    }

                    if (isoStore.FileExists("/GameLibrary/Database.xml"))
                    {
                        //isoStore.DeleteFile("/GameLibrary/Database.xml");
                        Database = await IsolatedStorageHelper.Load <List <KeyValue <int, GameData> > >("/GameLibrary/Database.xml");
                    }
                }

                if (Database != null)
                {
                    foreach (KeyValue <int, GameData> key in Database)
                    {
                        (DataContext as GameModel).MainGameView.GamesList.Add(key.Value);
                    }
                }

                Database = null;
            });
        }
        //Retrieve data from XAML elements, deserialize the database
        //Store the data into the dictionary and serialize the database

        //--still need to dispose the gamecover image--
        //--need to provide a way to save the images to a cache with size 99x99 and size 200 x 200--
        private async void SaveGameToDatabase(object sender, EventArgs e)
        {
            List <KeyValue <int, GameData> > Database;

            GameData data = null;

            if (sent_GameData != null)
            {
                data = sent_GameData;
            }

            else
            {
                Debug.WriteLine("Game was null");
                return;
            }

            using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!isoStore.DirectoryExists("/GameLibrary/"))
                {
                    isoStore.CreateDirectory("/GameLibrary/");
                }

                if (isoStore.FileExists("/GameLibrary/Database.xml"))
                {
                    Database = await IsolatedStorageHelper.Load <List <KeyValue <int, GameData> > >("/GameLibrary/Database.xml");
                }

                else
                {
                    Database = new List <KeyValue <int, GameData> >();
                }

                if (data.GameCoverCacheImage != null && data.GameCoverCacheImage.UriSource != null)
                {
                    SaveImageToPhone(data);
                }

                data.GameCover99Uri  = "/GameImages/" + data.GameID + "-" + data.GameTitle + "-cover-image-99.jpg";
                data.GameCover200Uri = "/GameImages/" + data.GameID + "-" + data.GameTitle + "-cover-image-200.jpg";
                data.GameCover       = null;

                Database.Add(new KeyValue <int, GameData>(data.GameID, data));

                await IsolatedStorageHelper.Save <List <KeyValue <int, GameData> > >(Database, "/GameLibrary/Database.xml");

                Database.Clear();
            }

            if (gameCover != null)
            {
                var bitmap = gameCover.Source as BitmapImage;

                if (bitmap != null)
                {
                    bitmap.UriSource = null;
                    gameCover.Source = null;
                }
            }

            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
        }