Пример #1
0
        private async void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
        {
            this.DefaultViewModel["Commands"] = this;

            var storage = new MenuCardStorage();
            MenuCardRepository.Instance.InitMenuCards(new ObservableCollection<MenuCard>(
                await storage.ReadMenuCardsAsync()));
            this.DefaultViewModel["Items"] = MenuCardRepository.Instance.Cards;
        }
Пример #2
0
        /// <summary>
        /// 保留与此页关联的状态,以防挂起应用程序或
        /// 从导航缓存中放弃此页。  值必须符合
        /// <see cref="SuspensionManager.SessionState"/> 的序列化要求。
        /// </summary>
        ///<param name="sender">事件的来源;通常为 <see cref="NavigationHelper"/></param>
        ///<param name="e">提供要使用可序列化状态填充的空字典
        ///的事件数据。</param>
        ///

        // 退出页面时会调用 保存添加的Card
        private async void navigationHelper_SaveState(object sender, SaveStateEventArgs e)
        {
            var mc = new MenuCard
            {
                Title = info.Title,
                Description = info.Description,
                Image = info.Image,
                ImagePath = info.ImageFileName
            };

            MenuCardRepository.Instance.Cards.Add(mc);
            var storage = new MenuCardStorage();
            await storage.WriteMenuCardsAsync(MenuCardRepository.Instance.Cards.ToList());
        }
Пример #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;
        }
Пример #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);
     }
 }