Ejemplo n.º 1
0
        // 从文件读取数据
        public async Task <IEnumerable <MenuCard> > ReadMenuCardsAsync()
        {
            var                    menuCards    = new List <MenuCard>();
            StorageFolder          folder       = ApplicationData.Current.RoamingFolder;
            StorageFileQueryResult result       = folder.CreateFileQuery();
            var                    queryOptions = new QueryOptions();

            queryOptions.IndexerOption = IndexerOption.DoNotUseIndexer;
            queryOptions.FolderDepth   = FolderDepth.Shallow;
            queryOptions.FileTypeFilter.Add(".xml");
            result.ApplyNewQueryOptions(queryOptions);
            IReadOnlyList <StorageFile> files = await result.GetFilesAsync();

            foreach (var file in files)
            {
                using (Stream stream = await file.OpenStreamForReadAsync())
                {
                    try
                    {
                        var    serializer = new DataContractSerializer(typeof(MenuCardData));
                        object data       = await Task <object> .Run(() => serializer.ReadObject(stream));

                        MenuCard menuCard = (data as MenuCardData).ToMenuCard();
                        menuCard.RestoreReferences();
                        menuCards.Add(menuCard);
                    }
                    catch (Exception)
                    {
                        // log exception
                    }
                }

                // read images
                var imageStorage = new MenuCardImageStorage();
                foreach (var menuCard in menuCards)
                {
                    if (menuCard.ImagePath != null)
                    {
                        menuCard.Image = await imageStorage.ReadImageAsync(menuCard.ImagePath);
                    }
                }
            }
            return(menuCards);
        }
Ejemplo n.º 2
0
        // 从文件读取数据
        public async Task<IEnumerable<MenuCard>> ReadMenuCardsAsync()
        {
            var menuCards = new List<MenuCard>();
            StorageFolder folder = ApplicationData.Current.RoamingFolder;
            StorageFileQueryResult result = folder.CreateFileQuery();
            var queryOptions = new QueryOptions();
            queryOptions.IndexerOption = IndexerOption.DoNotUseIndexer;
            queryOptions.FolderDepth = FolderDepth.Shallow;
            queryOptions.FileTypeFilter.Add(".xml");
            result.ApplyNewQueryOptions(queryOptions);
            IReadOnlyList<StorageFile> files = await result.GetFilesAsync();
            foreach (var file in files)
            {
                using (Stream stream = await file.OpenStreamForReadAsync())
                {
                    try
                    {
                        var serializer = new DataContractSerializer(typeof(MenuCardData));
                        object data = await Task<object>.Run(() => serializer.ReadObject(stream));

                        MenuCard menuCard = (data as MenuCardData).ToMenuCard();
                        menuCard.RestoreReferences();
                        menuCards.Add(menuCard);
                    }
                    catch (Exception)
                    {
                        // log exception
                    }
                }

                // read images
                var imageStorage = new MenuCardImageStorage();
                foreach (var menuCard in menuCards)
                {
                    if (menuCard.ImagePath != null)
                    {
                        menuCard.Image = await imageStorage.ReadImageAsync(menuCard.ImagePath);
                    }
                }
            }
            return menuCards;
        }
Ejemplo n.º 3
0
        private async void OnUploadImage(object sender, RoutedEventArgs e)
        {
            // 定义选择器
            var filePicker = new FileOpenPicker();
            filePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            filePicker.FileTypeFilter.Add(".jpg");
            filePicker.FileTypeFilter.Add(".png");
            StorageFile file = await filePicker.PickSingleFileAsync();
            if (file == null) return;

            var stream = await file.OpenAsync(FileAccessMode.Read);
            var image = new BitmapImage();

            image.SetSource(stream);
            image.ImageOpened += async (sender1, e1) =>
            {
                if (image.PixelHeight > image.PixelWidth)
                {
                    image.DecodePixelHeight = 900;
                }
                else
                {
                    image.DecodePixelWidth = 900;
                }
                stream.Seek(0);
                var imageStorage = new MenuCardImageStorage();
                var storage = new MenuCardStorage();
                info.ImageFileName = string.Format("{0}.jpg", Guid.NewGuid().ToString());

                await imageStorage.WriteImageAsync(stream, info.ImageFileName);
            };
            image.ImageFailed += (sender1, e1) =>
            {
                string s = e1.ErrorMessage;
            };
            info.Image = image;
        }
Ejemplo n.º 4
0
 private static async Task InitSampleDataAsync()
 {
     var storage = new MenuCardStorage();
     var imageStorage = new MenuCardImageStorage();
     if (await storage.IsRoamingFolderEmpty())
     {
         List<MenuCard> menuCards = MenuCardRepository.GetSampleMenuCards().ToList();
         foreach (var card in menuCards)
         {
             RandomAccessStreamReference streamRef =
               RandomAccessStreamReference.CreateFromUri(new Uri(card.ImagePath));
             using (IRandomAccessStreamWithContentType stream =
               await streamRef.OpenReadAsync())
             {
                 card.ImagePath = string.Format("{0}.png", Guid.NewGuid());
                 await imageStorage.WriteImageAsync(stream, card.ImagePath);
             }
         }
         await storage.WriteMenuCardsAsync(menuCards);
     }
 }